Linux Virtual Memory - overcommit


Bhaskar S 09/06/2013


Recently ran into an interesting behavior in Linux.

What happens when you dynamically allocate memory by calling the function malloc() and there is no more memory to allocate ?

One would expect the malloc() function to return a NULL, correct ?

Guess what - the default behavior of the malloc() function in Linux is to always succeed and return a valid pointer - ain't this INTERESTING !!!

Why this behavior ?

Linux does lazy memory allocation and calling the function malloc() does not immediately allocate physical memory. It only returns a pointer to the start of the virtual memory with the assumption that it may not be actually used.

The Linux kernel allocates real physical memory upon the first use of the pointer from the malloc() function call.

This behavior implies that the Linux kernel will allow programs to allocate more memory than is actually available.

This feature in Linux is called the memory overcommit.

What happens when a program tries to use the virtual memory pointer and there is no more physical memory available ???

The Linux kernel kickstarts the Out-Of-Memory (OOM) killer process, which uses heuristics to kill one of the user-level programs to free more memory.

WOW - this is a bit scary and a cause for concern as a random user process could be killed.

If we execute the following command:

$ cat /proc/sys/vm/overcommit_memory

The following is the output:

Output

0

The value of the kernel parameter overcommit_memory can take one of the three following values:

To change the the kernel parameter overcommit_memory value to 2, execute the following command:

$ sudo vi /etc/sysctl.conf

At the end of the file add the following two lines:

vm.overcommit_memory = 2

vm.overcommit_ratio = 80

Save the changes and reboot the system and we are done.