The OOM killer (out-of-memory killer) is the Linux kernel mechanism that steps in when a system runs out of memory and cannot reclaim any more: it picks a process and kills it so the machine can keep running. Admins know the symptoms well: a database that vanishes overnight, a container that restarts with nothing in its own logs, or a critical service that dies while the real memory hog keeps running. Knowing how it decides is the first step to preventing it and steering it.
The Linux OOM killer in 30 seconds
- It runs when the kernel cannot satisfy an allocation even after dropping caches and using swap.
- It kills the process with the highest badness score, which mostly reflects how much memory it uses.
- You tune it with oom_score_adj (-1000 to 1000) or OOMScoreAdjust= in systemd units, and contain services with cgroup memory limits.
- systemd-oomd uses pressure stall information to act earlier, before the machine grinds to a halt.
By default Linux lets processes reserve more memory than physically exists, because many programs allocate memory “just in case” and never touch most of it. This is overcommit. Trouble starts when processes actually use that memory at the same time: the kernel tries to reclaim pages by dropping cache and swapping, and if that fails, it calls the OOM killer.
How it chooses a victim
The kernel gives each process a badness score from 0 to 1000 that essentially reflects the share of available memory it uses: resident memory, page tables and swap. The process with the highest score is killed. You can read it in /proc/PID/oom_score, which already includes any manual adjustment.
That adjustment lives in /proc/PID/oom_score_adj, which accepts values from -1000 to 1000 and is added to the score. At -1000 the process is exempt; at 1000 it will almost always go first. The old oom_adj interface, with values from -17 to 15, has been deprecated since oom_score_adj arrived in Linux 2.6.36 and is kept only for compatibility.
# Processes with the highest OOM score
for p in /proc/[0-9]*; do
printf "%s %s %sn" "$(cat $p/oom_score 2>/dev/null)" "${p#/proc/}" "$(cat $p/comm 2>/dev/null)"
done | sort -rn | head -10
# Check and adjust one process
cat /proc/1234/oom_score_adj
echo -500 | sudo tee /proc/1234/oom_score_adjCode language: PHP (php)
How to tell it fired
The OOM killer leaves a trail in the kernel log: the task that triggered it, a table of processes with their memory use, and the one that was killed.
sudo dmesg -T | grep -iE "out of memory|oom-kill|killed process"
sudo journalctl -k --since "24 hours ago" | grep -i oom
# With cgroup v2 (containers and systemd services)
cat /sys/fs/cgroup/system.slice/my-service.service/memory.eventsCode language: PHP (php)
In memory.events, the oom and oom_kill counters show how often the cgroup hit its memory limit and how many processes were killed. Tell a global OOM, where the whole machine runs out, apart from a cgroup OOM, where a container or service exceeds its own limit even though the host has free memory. In Kubernetes and Docker, the second case is the usual one and shows up as OOMKilled.
Protecting services and acting earlier
Writes to /proc do not survive a process restart. For services managed by systemd, set the value in the unit with OOMScoreAdjust=, and cap memory with MemoryMax= so a runaway service cannot take the host down. OOMPolicy= decides what systemd does with the rest of the service when the kernel kills one of its processes.
sudo systemctl edit postgresql.service
[Service]
OOMScoreAdjust=-500
MemoryMax=12G
Exempting a process with -1000 has a cost: if it cannot die, the kernel kills something else, and if that process is the one eating memory, you can end up with a wedged system. Keep it for small, essential processes such as remote management agents.
The kernel often acts late: before it kills anything, a box under memory pressure can spend minutes unresponsive while it drops cache and swaps. Userspace tools exist to act sooner. systemd-oomd uses the kernel’s pressure stall information (PSI), available since Linux 4.20, together with cgroup v2 to kill a whole cgroup when memory pressure or swap use stays above a threshold for a set time; enable it per unit with ManagedOOMMemoryPressure=kill and inspect it with oomctl. Fedora enables it by default. Current pressure is visible in /proc/pressure/memory.
System-wide knobs exist too, but handle them with care: vm.overcommit_memory=2 turns off overcommit so allocations fail instead of invoking the OOM killer, which many applications handle poorly, and vm.panic_on_oom panics the kernel instead of killing processes, which only makes sense in clusters where rebooting a node is preferable. On most servers, the effective fix is sizing memory properly, setting per-service limits and watching pressure before the kernel has to choose.
FAQ
What is the OOM killer?
The Linux kernel mechanism that kills a process when the system runs out of memory and cannot reclaim more, so the machine does not lock up.
How do I stop it from killing a specific service?
Lower its score with OOMScoreAdjust= in the systemd unit or /proc/PID/oom_score_adj, and cap the memory of other services.
How do I know the OOM killer killed a process?
Search dmesg or journalctl -k for “Out of memory” or “Killed process”, and check the oom_kill counter in the service cgroup’s memory.events.
Sources:
