Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Linux 上,如果内存不足,malloc 不一定会返回空指针。如果你真的内存不足,你可能会取回一个指针,然后让 OOM 杀手开始吃进程。c++ 的运算符 new 是否也是如此,还是会得到 bad_alloc 异常?
它是一个内核函数而不是一个语言函数——你可以使用vm.overcommit_memory和vm.overcommit_ratiosysctls 来控制它。
vm.overcommit_memory
vm.overcommit_ratio
它们在 proc 文件系统中可见/proc/sys/vm/overcommit_memory和/proc/sys/vm/overcommit_ratio。
/proc/sys/vm/overcommit_memory
/proc/sys/vm/overcommit_ratio
运算符 new 也是如此,唉 :^(