Skip to content

Lesson 5 of 14 · 20m

Resource limits and what the OOM killer actually does

MemoryMax, CPUQuota, TasksMax, OOMScoreAdjust — bound each service's blast radius before the kernel picks a victim.

The OOM killer doesn't ask which process matters. It walks oom_score, picks the biggest cheapest kill, and fires — and it usually picks the process you were least willing to lose, because the database holds more memory than the leaky worker that caused the problem. The wrong mental model is "OOM means buy RAM." OOM is a scheduling decision the kernel made because nobody told it your priorities. cgroups v2 and a few [Service] directives are how you tell it.

What happens when memory runs out

The kernel's OOM killer scores every process — roughly resident size, adjusted by oom_score_adj (−1000 to 1000) — and kills the top scorer. You see it in the kernel log, not the victim's own log:

journalctl -k -g 'oom|Out of memory'
journalctl -u app --since "-1h"   # ends abruptly; the unit records "killed by the OOM killer"

A unit whose log just stops, with no error, was usually killed by something external — check journalctl -k for oom-kill and -p err -b around the timestamp before you blame the code.

Bound the blast radius per-service

[Service]
MemoryMax=1G
MemoryHigh=800M
CPUQuota=200%
IOWeight=100
TasksMax=200
OOMScoreAdjust=500
  • MemoryMax= is the hard cap — the cgroup hits it and the service's processes OOM together, inside their own boundary; the rest of the box stays alive. MemoryHigh= starts reclaim pressure earlier, so most services throttle before they die.
  • CPUQuota=200% caps the service at two cores' worth. A runaway npm ci or a forked-away worker can't eat all 32.
  • TasksMax= bounds fork bombs and thread storms — a service that normally holds 30 threads shouldn't get 3,000.
  • OOMScoreAdjust=500 biases the kernel killer toward this service; negative values protect it. Put +500 on expendable batch workers, leave databases at 0 or below.
  • IOWeight= throttles a backup or compaction job that would otherwise starve the database's disk.

Set it live without editing the unit: systemctl set-property app.service MemoryMax=1G. Inspect live: systemd-cgtop shows per-cgroup CPU/memory/IO — top for the box's services, which is the granularity ops actually needs.

systemctl status app.service already prints a Memory: 412M line when accounting is on — older builds need DefaultMemoryAccounting=yes in /etc/systemd/system.conf or per-unit MemoryAccounting=yes for systemd-cgtop and the status line to stop guessing. The number you want before writing MemoryMax is the service's honest steady-state plus headroom, not a round number from a diagram.

systemd-oomd and earlyoom

The kernel OOMs late — after minutes of thrashing. systemd-oomd (Fedora ships it enabled; Debian/Ubuntu package it but default off) kills a cgroup when memory pressure (PSI) crosses thresholds, long before the kernel's panic-driven pass. earlyoom is the lighter-weight alternative. Either beats "the box hung for twenty minutes and then killed postgres anyway."

Try this

On a scratch box: systemctl set-property stress.service MemoryMax=200M, run stress-ng --vm 1 --vm-bytes 1G inside it, and watch journalctl -k fire on the cgroup while the box stays healthy. Then run systemd-cgtop during a normal workload — you'll find at least one service quietly using three times what anyone estimated.

Pitfalls

  • MemoryMax on a service with a legitimate cache — the kernel kills it for doing its job. MemoryHigh plus monitoring beats a hard cap you can't size yet.
  • An OOM inside the cgroup doesn't always say "oom" in the app log — allocation failures surface as random errors. Correlate with journalctl -k before debugging the wrong layer.
  • OOMScoreAdjust is a bias, not a guarantee — protect the wrong thing and the killer still picks postgres.
  • cgroup v1 vs v2 naming differs on older distros (MemoryMax vs MemoryLimit). stat -fc %T /sys/fs/cgroup prints cgroup2fs when you're on v2.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.