Linux optimistic malloc: will new always throw when out of memory?

It depends; you can configure the kernel’s overcommit settings using vm.overcommit_memory.

Herb Sutter discussed a few years ago how this behavior is actually nonconforming to the C++ standard:

“On some operating systems, including specifically Linux, memory allocation always succeeds. Full stop. How can allocation always succeed, even when the requested memory really isn’t available? The reason is that the allocation itself merely records a request for the memory; under the covers, the (physical or virtual) memory is not actually committed to the requesting process, with real backing store, until the memory is actually used.

“Note that, if new uses the operating system’s facilities directly, then new will always succeed but any later innocent code like buf[100] = ‘c’; can throw or fail or halt. From a Standard C++ point of view, both effects are nonconforming, because the C++ standard requires that if new can’t commit enough memory it must fail (this doesn’t), and that code like buf[100] = ‘c’ shouldn’t throw an exception or otherwise fail (this might).”

Leave a Comment