Linux memory overcommit

Linux memory overcommit

Linux is generous in terms of memory, it will almost never fail on requests from malloc(3) with friends. What does this mean in practice and how may it be a potential issue?

In short, overcommit memory means that the system will give the application so much memory it is asking for, even if the physical memory is not available. How may this work? Well, the requested memory comes with one small restriction; the application is given as much memory it demands if it not going to use it. Seriously? Yes, and it is pretty clever too.

The main purpose is to optimize memory handling by avoid swapping out memory as much as possible. The application does not _really_ need the memory before it is going to use it anyway (if it's going to be used it at all), and it is not unlikely that an other application has freed memory before the allocated memory is used. A swap has been avoided.

Now, think about an embedded system without a swap area and with a limited amount of memory. Is memory overcommit still a good thing? It could be. It could also be a treacherous, unpredictable demon that haunts seemingly random devices.

In a case that the application uses a library that allocates tons of memory but never going to use it all, memory overcommit is pretty good because the application may not even start without it. A weird example? Not at all, let me just say three words; Qt with QML.

On the other hand, if the application really intend to use the memory we have a problem It is even worse if the application only use the memory under specific circumstances that is hard to track down.

If a system is running out of memory, the unforgiving Out Of Memory- (OOM-) killer will terminate a random (well.. not actually random, but lets call it that) application in desperation. This randomness makes it a little bit tricky. The victim may be your SSH server, your logging server, your application or whatever stands in the way of the OOM-killer.

The Linux kernel supports the following overcommit handling modes (refer to Documentation/vm/overcommit-accounting)

In practice

The overcommit policy is set via the sysctl vm.overcommit_memory or by writing to /proc/sys/vm/overcommit_memory.

For example:

echo 2 | sudo tee /proc/sys/vm/overcommit_memory