Skip to content

Why the OOM killer took out the wrong process

A process you weren't watching got killed under memory pressure. Here is how the OOM killer picks its target, and how to make it pick the one you want.

A node goes quiet for two minutes. When it comes back, your database is fine but the metrics agent is gone — or worse, sshd is gone and you need the cloud provider’s serial console to get back in. The kernel ran out of memory, picked a victim, and killed it. The victim was rarely the actual culprit.

What’s happening

When an allocation cannot be satisfied and reclaim has failed, the kernel walks the process tree and computes an oom_score for each candidate. The score is roughly proportional to RSS plus a few biases, and operators can shift it with oom_score_adj (range -1000 to +1000). A long-running JVM with a 12 GB heap will almost always outscore a small misbehaving cron. “Just add swap” does not fix this — swap delays the moment the killer fires but rarely changes the outcome, and on a host already paging hard, thrash makes everything slower while the OOM still happens.

A cgroup with a memory limit and memory.oom.group=1 flips the picture: the entire cgroup is killed together, not one process within it. That is usually what you want for a container — a half-killed container is a worse failure mode than a full restart.

How to verify

The kernel always leaves a trail. Read both:

dmesg -T | grep -i 'killed process\|out of memory\|oom'
journalctl -k --since "2 hours ago" | grep -i oom

The line you want looks like Out of memory: Killed process 12345 (mysqld) total-vm:... anon-rss:... oom_score_adj:0. The oom_score_adj value tells you whether anything had nudged it up or down. Inspect the current score for a live process:

cat /proc/$(pidof myapp)/oom_score
cat /proc/$(pidof myapp)/oom_score_adj

Confirm there is no accidental panic policy that turns OOM into a full host reboot:

sysctl vm.panic_on_oom vm.overcommit_memory vm.overcommit_ratio

For cgroup-scoped events on a systemd host:

systemd-cgtop -m
journalctl -u myapp.service --grep 'oom\|killed'

The fix

Identify what should stay alive, and tell the kernel:

# /etc/systemd/system/sshd.service.d/oom.conf
[Service]
OOMScoreAdjust=-900

Drop in the same for any agent you cannot afford to lose. Conversely, mark a known memory hog more killable with OOMScoreAdjust=500.

For containerised workloads, set a real memory limit on the cgroup and enable group-kill, so a runaway worker takes its siblings with it and the supervisor restarts the lot cleanly rather than leaving half-dead state behind.

Then look at the actual cause. The OOM is a symptom — a leak, an unbounded cache, an under-sized host, a query loading a whole table into memory. Sizing the host or tuning the application is the real remediation; score adjustments only decide who dies first.

If OOM events have shown up more than once and the pattern is not yet clear, that is the kind of investigation Monitoring & Support is built to run — reach out.