Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What the OOM Killer Does

Programs ask the kernel for memory. When the kernel cannot give more, because RAM is full and swap is full or absent, it has two choices: freeze or end something. It ends one process, usually the one that uses the most memory and is not marked as protected, and frees its memory at once. The process gets no warning and cannot save its work. A database, a web application, or a PHP worker pool is often the one chosen, because it is usually the largest.

Find the Kernel's Record

sudo dmesg -T | grep -i -E "out of memory|killed process|oom"
sudo journalctl -k | grep -i -E "out of memory|killed process"
sudo journalctl -k -b -1 | grep -i oom

The first two search the current boot; the third searches the boot before, if the journal is kept on disk. dmesg times can drift; the journal shows the exact time. See the dmesg command for both.

A killed service also shows it. systemctl status for that unit may report that it failed with the result oom-kill, and its own log stops in the middle of work:

sudo systemctl status mariadb
sudo journalctl -u mariadb --since today

Read the Record

An OOM event writes a block of lines, not one. In general terms it contains:

  • The trigger: a line naming the process that asked for memory and "invoked" the OOM killer. This is not always the process that was killed.
  • Memory state: a summary of memory and swap at that moment.
  • A task table: the processes considered, with their memory use and OOM score adjustment.
  • The result: "Out of memory" and "Killed process", with the PID, name, and how much memory it held.

If the record mentions a memory cgroup, the limit that was reached belonged to one service or container, not to the whole server.

How the Victim Is Chosen

Each process has a score, based on the memory it holds in RAM, in swap, and in page tables. You can see it for any PID:

cat /proc/1234/oom_score
cat /proc/1234/oom_score_adj

oom_score_adj runs from -1000 to 1000 and shifts the score; -1000 turns OOM killing off for that process.

Find What Used the Memory

  1. Check the task table in the record: the largest entries are the suspects.
  2. Check current use with free -h and ps -eo pid,rss,comm --sort=-rss | head; see how to check memory usage.
  3. Watch a suspect over hours. Memory that only grows and never falls points to a leak.
  4. For the time before the event, read history from atop if it was recording.

The Real Fixes

  • Limit the service: set its own memory settings, such as the database buffer pool size or the number of PHP-FPM workers, so the total fits in RAM.
  • Cap it in systemd: MemoryMax= in the unit is a hard limit for one service; when it is reached, the OOM killer acts inside that service instead of the whole server running out. MemoryHigh=, set lower, slows the service and reclaims its memory first.
  • Fix the leak: update or fix the program; restarting it on a schedule only hides the leak.
  • Add memory: if normal work does not fit, the server needs more RAM.
  • Add swap as a cushion: it absorbs short peaks, but a server that lives in swap is slow. See how to add a swap file.

Protect an Important Service

Set the adjustment in the service's systemd unit, so it applies every time the service starts. Do not write to /proc by hand: the value is lost when the process restarts.

sudo systemctl edit mariadb

Add these lines in the editor and save. systemctl edit reloads the systemd configuration by itself; the new value reaches the service only after sudo systemctl restart mariadb, so restart it at a quiet time:

[Service]
OOMScoreAdjust=-500

Use this sparingly. Protecting a process means another one is killed instead, and if the protected process is the one using all the memory, the problem stays. Drop-in files and other unit settings are explained in systemd unit files.

Was this answer helpful?
Loading...