Memory Kernel – How Does vm.overcommit_memory Work?

kernelmemorysysctl

When I use the default settings:

vm.overcommit_memory = 0
vm.overcommit_ratio = 50

I can read these values from /proc/meminfo file:

CommitLimit:     2609604 kB
Committed_AS:    1579976 kB

But when I change vm.overcommit_memory from 0 to 2, I'm unable to start the same set of applications that I could start before the change, especially amarok. I had to change vm.overcommit_ratio to 300, so the limit could be increased. Now when I start amarok, /proc/meminfo shows the following:

CommitLimit:     5171884 kB
Committed_AS:    3929668 kB

This machine has only 1GiB of RAM, but amarok works without problems when vm.overcommit_memory is set to 0. But in the case of setting it to 2, amarok needs to allocate over 2GiB of memory. Is it a normal behavior? If so, could anyone explain why, for instance, firefox (which consumes 4-6x more memory than amarok) works in the same way before and after the change?

Best Answer

You can find the documentation in man 5 proc (or at kernel.org):

/proc/sys/vm/overcommit_memory
       This file contains the kernel virtual memory accounting mode.
       Values are:

              0: heuristic overcommit (this is the default)
              1: always overcommit, never check
              2: always check, never overcommit

       In mode 0, calls of mmap(2) with MAP_NORESERVE are not
       checked, and the default check is very weak, leading to the
       risk of getting a process "OOM-killed".

       In mode 2 (available since Linux 2.6), the total virtual
       address space that can be allocated (CommitLimit in /proc/mem‐
       info) is calculated as

           CommitLimit = (total_RAM - total_huge_TLB) *
                         overcommit_ratio / 100 + total_swap

The simple answer is that setting overcommit to 1, will set the stage so that when a program calls something like malloc() to allocate a chunk of memory (man 3 malloc), it will always succeed regardless if the system knows it will not have all the memory that is being asked for.

The underlying concept to understand is the idea of virtual memory. Programs see a virtual address space that may, or may not, be mapped to actual physical memory. By disabling overcommit checking, you tell the OS to just assume that there is always enough physical memory to backup the virtual space.

Example

To highlight why this can sometimes matter, take a look at the Redis guidances on why vm.overcommit_memory should be set to 1 for it.