GNU+Linux command memo

No swap but no freeze

If you turn off your swap (to spare save some storage for instance), you might get stranded with an unresponsive computer as soon as Firefox or LibreOffice eats all your RAM. Here is how to avoid this, allowing the Linux kernel to kill your guilty process via its OOM-Killer (Out of Memory Killer) before getting stranded.

Si vous désactivez votre swap (pour économiser du stockage par exemple) vous risquez d’être bloqué avec un ordinateur ne répondant plus dès que Firefox ou LibreOffice aura mangé toute la mémoire RAM. Voici comment éviter ça, en incitant le noyau Linux à tuer le processus le plus gourmand via son OOM-Killer (Out of Memory Killer) avant le blocage.

$ sudo sysctl vm.swappiness=0 (1)
$ sudo sysctl vm.vfs_cache_pressure=200 (2)
$ sudo sysctl vm.overcommit_memory=1 (3)
1 Tells the kernel that there is no swap (seems overkill)
2 Tells the kernel to sacrifice cache over user application memory requests
3 No overcommit of the memory, so the OOM killer will invoked as soon as memory misses

To keep those settings over a reboot :

$ sudo echo "vm.swappiness=0" >> /etc/sysctl.conf
$ sudo echo "vm.vfs_cache_pressure=200" >> /etc/sysctl.conf
$ sudo echo "vm.overcommit_memory=1" >> /etc/sysctl.conf

As a consequence, you may keep an eye on memory usage and restart Firefox or close unused applications to free some memory and avoid loosing your current LibreOffice work when the OOM-Killer start working. Although Firefox and LibreOffice don’t suffer too much from an impromptu closing.

1. P.S. 2020-04-11 earlyoom :

Well, to be true, the OOM-Killer of the Linux kernel (4.19 - 5.4) does freeze the system for several minutes before it actually free the needed memory. When you’re watching a movie on the machine it’s a problem.

A solution exists ! It’s earlyoom. It’s packaged in Debian stable Buster (10). Easy to install and easy to configure through /etc/default/earlyoom. You can prevent this daemon from killing your init sshd or X servers. But it will kill too big tabs in Firefox effectively before the kernel OOM-Killer.