Pages

Sunday, June 13, 2021

Fedora 34 : Linux tool to prevent OOM Killer.

The OOM Killer or Out of Memory Killer is a process that the Linux kernel employs when the system is critically low on memory
This situation occurs because processes on the server are consuming a large amount of memory, and the system requires more memory for its processes and to allocate to other processes.
A simple example with OOM Killer: if will swap out the desktop environment, drop the whole page cache, and empty every buffer before it will ultimately kill a process.
This tool named earlyoom is a simple, stable, and tiny OOM prevention daemon.
That is written in C and is the best choice for old servers.
It has minimum dependencies and can work with the oldest kernels.
The earlyoom checks the amount of available memory and free swap up.
This command causes memory consumption, and the process will exits before OOM occurs if this tool works.
The tool can be installed with the following command:
[root@desk mythcat]# dnf install earlyoom
Last metadata expiration check: 0:38:00 ago on Sun 13 Jun 2021 11:38:22 AM EEST.
Package earlyoom-1.6.2-3.fc34.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
You can see all options for this tool with this command:
[root@desk mythcat]# earlyoom -h
earlyoom 1.6.2
Usage: earlyoom [OPTION]...
Then the service is activated with:
[root@desk mythcat]# systemctl enable --now earlyoom
My configuration file for this tool is:
[root@desk mythcat]# vim  /etc/default/earlyoom
EARLYOOM_ARGS="-r 1 -m 1 -s 5 -M 1514244 --prefer '^Web Content$' 
--avoid '^(dnf|packagekitd|gnome-shell|gnome-session-c|gnome-session-b|lightdm|
sddm|sddm-helper|gdm|gdm-wayland-ses|gdm-session-wor|gdm-x-session|Xorg|Xwayland|
systemd|systemd-logind|dbus-daemon|dbus-broker|cinnamon|cinnamon-sessio|kwin_x11|
kwin_wayland|plasmashell|ksmserver|plasma_session|startplasma-way|xfce4-session|
mate-session|marco|lxqt-session|openbox|cryptsetup)$'"
You can check the status of the service to be active:
[root@desk mythcat]# systemctl status earlyoom
...
Active: active (running) since 
... 
You can test it easily with these commands:
[root@desk mythcat]# sudo swapoff -a
[root@desk mythcat]# tail /dev/zero
Terminated
The process tail must occupy the entire available RAM on the system, this will low your system and after the earlyoom tool freeing the RAM all works well.
To see the tool on work, use the command:
[root@desk mythcat]# earlyoom 
earlyoom 1.6.2
mem total: 7899 MiB, swap total: 7898 MiB
sending SIGTERM when mem <= 10.00% and swap <= 10.00%,
        SIGKILL when mem <=  5.00% and swap <=  5.00%
mem avail:  4742 of  7899 MiB (60.04%), swap free: 7898 of 7898 MiB (100.00%)
mem avail:  4742 of  7899 MiB (60.04%), swap free: 7898 of 7898 MiB (100.00%)
mem avail:  4751 of  7899 MiB (60.15%), swap free: 7898 of 7898 MiB (100.00%)
mem avail:  4754 of  7899 MiB (60.19%), swap free: 7898 of 7898 MiB (100.00%)