What is OOM Killer?

Out-of-Memory (OOM) Killer is a utility that kills out-of-memory processes. It works by sending a SIGKILL signal to the offending process rather than killing it directly. Out-of-Memory is a kernel error message that occurs when the system runs out of memory, after which programs stop working.

When running a Linux server, you might want to run a service, such as Apache, MySQL, etc., on a single computer. But you may not always have enough memory to allocate to these services. Additionally, if you leave them idle, they consume more memory than you have allocated. In this case, the operating system kills processes to free up memory, and this is known as the Out-of-Memory (OOM) killer.

The kernel uses the OOM (Out Of Memory) killer in Linux Servers to address critical memory shortages. When the system depletes its memory resources, the Linux OOM killer intervenes, terminating one or more processes to reclaim memory when other measures have proven ineffective.

This situation arises due to processes on the server consuming a significant amount of memory, leading to additional memory allocation to prioritize other essential processes and maintain the system's functionality.

The Linux kernel utilizes a strategy involving the OOM Killer. This mechanism evaluates all active processes and terminates one or more to free up system memory and keep the system running. A specific heuristic/algorithm is employed to identify the most suitable process that can have its memory freed without damaging the fundamental operation of the system.

Why is Apache / MySQL/Postgres always Killed?

When operating a Linux server, you may run services such as Apache, MySQL, etc., on a single machine. However, there may only sometimes be sufficient memory for these services. Moreover, if left idle, they can consume more memory than allocated. In such conditions, the operating system employs the Out-of-Memory (OOM) killer, which terminates processes to release memory and prevent system resource depletion.

How does it work

The OOM Killer determines all active processes and assigns a badness score to them. The process with the highest score is the one selected for termination. The OOM Killer determines a badness score by considering several criteria, primarily including the following:

Processes and their child processes excessively consume memory, either by requesting more than available or withholding unused memory from reclamation.

The priority is to kill the minimum number of processes (ideally one).

Root and essential system processes are typically given lower badness scores by default.

When one or more processes are selected, the OOM-Killer invokes the oom_kill_task() function, which is responsible for sending the terminate/kill signal to the process. In the event of an out-of-memory situation, the oom_kill() calls this function to send the SIGKILL signal to the process, and a kernel log generates a message.

Out of Memory: Killed process [pid] [name].  

Why do we use OOM Killer?

Implementing OOM Killer reduces the risk of Linux server crashes caused by resource exhaustion. It is particularly advantageous for servers engaged in critical tasks such as web serving or database management.

How to manage OOM-Killer?

Turning off the OOM-Killer is generally not recommended, but Linux can turn it on/off. The kernel parameter "vm.oom_kill" manages the OOM-Killer. If you wish to enable the OOM-Killer at runtime, you can utilize the "sysctl" command.

sudo -s sysctl -w vm.oom-kill = 1

To disable the OOM-killer run the following command:

sudo -s sysctl -w vm.oom-kill = 0

An alternative method to turn the OOM Killer on or off is by setting the "panic_on_oom" variable.

# cat /proc/sys/vm/panic_on_oom

0

Here, value 0 means the kernel will not panic when an out-of-memory error occurs.

$ echo 0 > /proc/sys/vm/panic_on_oom

Here, value 1 means the kernel will panic when an out-of-memory error occurs.

echo 1 > /proc/sys/vm/panic_on_oom

Note:

To edit this file, root permission is required. The given command only sets the configuration temporarily, and a machine reboot will reset it. Add the following line to the /etc/sysctl.conf file to set the configuration permanently:

echo vm.oom-kill = 1 >>/etc/sysctl.conf

Conclusion

The killer is not always harmful. This mechanism safeguards your system. The OOM-Killer identifies and terminates the most problematic process, preventing system crashes and ensuring the system's integrity.

Was this answer helpful? 0 Users Found This Useful (0 Votes)