27.2.1.2. malloc maximum size

See also:

From Memory size and ./run --help, we see that at we set the emulator memory by default to 256MB. Let’s see how much Linux allows us to malloc.

Then from malloc implementation we see that malloc is implemented with mmap. Therefore, let’s simplify the problam and try to understand what is the larges mmap we can do first. This way we can ignore how glibc implements malloc for now.

In Linux, the maximum mmap value in controlled by:

cat /proc/sys/vm/overcommit_memory

which is documented in man proc.

The default value is 0, which I can’t find a precise documentation for. 2 is precisely documented but I’m lazy to do all calculations. So let’s just verify 0 vs 1 by trying to mmap 1GiB of memory:

echo 0 > /proc/sys/vm/overcommit_memory
./linux/mmap_anonymous.out 0x40000000
echo 1 > /proc/sys/vm/overcommit_memory
./linux/mmap_anonymous.out 0x40000000

With 0, we get a failure:

mmap: Cannot allocate memory

but with 1 the allocation works.

We are allowed to allocate more than the actual memory + swap because the memory is only virtual, as explained at: https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management/57453334#57453334

If we start using the pages, the OOM killer would sooner or later step in and kill our process: Linux out-of-memory killer.