Recent findings suggest that at 03:12 the kernel killed your SSH session instead of the service that was eating 14 GB. Nothing was broken. Nothing was limited either. The whole machine was one big cgroup, and the OOM killer picked a victim by badness score, not by blame.
That is the default on most Linux systems: every process competes for the same pool of memory. When the pool runs dry, the kernel chooses something to kill. It might choose the leak. It might choose your database. It might choose sshd.
Recent findings suggest that cgroup v2 gives you a better answer than "hope the score is right". You can put a ceiling on a service, make it slow down before it gets there, and read exactly what happened afterwards. The knobs are small and the semantics are not obvious. This article is about the semantics. (Related: Latest Update: LA Noire 2 leak resurfaces after source ...)
According to recent reports, the lab below was run on Debian 13 (kernel 6.12, systemd 257). The cgroup files are the same on any system using the unified hierarchy. (Related: Top Democrat asks Trump administration to stop using di...)
Recent findings suggest that if that prints cgroup2fs, you are on cgroup v2 and everything below applies. If it prints tmpfs, you are on the old hierarchy and the file names are different (memory.limit_in_bytes, memory.memsw.limit_in_bytes). Check before copying any of this.
Also check that the memory controller is actually available:.
If memory is missing, the controller is disabled in the kernel command line or not compiled in, and none of the unit settings will do anything.
Industry observers note that every cgroup has a memory.current file. It is the total memory at the moment charged to that cgroup and its descendants, in bytes.
That number is not RSS. It includes:.
memory.stat breaks it down. The two lines you will use most are anon and file:.
According to recent reports, this distinction matters because file is mostly reclaimable. A service that reads 2 GB of logs can show 2 GB in memory.current while being under no real pressure: the kernel can throw those pages away and read them again later. A service whose anon grows by 200 MB per hour has a leak, and no amount of cache dropping will fix it.
The kernel documentation is explicit that file includes tmpfs and shared memory. That is worth remembering the next time a service with a small RSS hits its limit because something wrote 500 MB to /dev/shm.
There are five memory files you will actually use, plus three for observation.
The vital pair is memory.high and memory.max, because people treat them as interchangeable and they are not.
memory.high is a speed limit. When usage goes over it, the kernel does not kill anything. It applies heavy reclaim pressure and throttles the tasks that are trying to charge more memory. The kernel documentation is blunt about the guarantee: going over the high limit never invokes the OOM killer, and under extreme conditions the limit may be breached. memory.events counts these episodes in the high field.
memory.max is a wall. When usage reaches it and reclaim cannot bring it down, the OOM killer is invoked in that cgroup. The doc again: "If a cgroup's memory usage reaches this limit and can't be reduced, the OOM killer is invoked in the cgroup." It also warns that usage may go over the limit temporarily, because accounting and allocation are not atomic.
That is the whole design: high buys you time and backpressure; max is the last line of defense. systemd's own documentation recommends exactly that split, using MemoryHigh= as the main control and MemoryMax= as the fallback.
cgroup v2 has a no-internal-process rule: a non-root cgroup can contain processes or child cgroups, not both. systemd builds slices (which contain units) and services (which contain processes) on top of that rule.
According to recent reports, the practical consequence is that a limit is never just yours. If myapp.service sits under myapp.slice, and the slice is capped at 2 GB, then a 4 GB MemoryMax= on the service is not reachable. systemctl show exposes the result:.
EffectiveMemoryMax is the most stringent limit of the unit and its parent slices, capped by physical memory. When a limit "does not work", this is the first field to read.
Recent findings suggest that here is a real experiment. It uses a transient user scope so nothing permanent is created:.
Inspect what systemd actually applied:.
Sources indicate that the numbers are bytes and the suffixes are base 1024: 384M is 402653184. Now read the cgroup files directly, using the path systemd reported:.
Nothing controversial so far: a sleeping process, three limits, and an empty event counter.
Now the interesting part. Allocate 200 MB of anonymous memory inside a scope limited to 256 MB, with swap disabled:.
84 milliseconds. The allocation fits, the limit is never approached, and nothing is throttled.
Repeat it with MemoryHigh=128M added, while keeping MemoryMax=256M:.
This is where the intuition breaks. The process did not get killed. It also did not finish. After 58 seconds it was still allocating, sitting in uninterruptible sleep (D state) inside the kernel's reclaim path. The cgroup counters told the story:.
494 throttle events, zero OOM kills. The process had charged about 186 MB, well above the 128 MB high limit, and the kernel kept throttling it instead of killing it. I stopped the unit at 58 seconds; it had not printed its completion line.
Two lessons, and they are the reason this section exists:.
In a recent development, if your goal is "never exceed X", use MemoryMax. If your goal is "slow down before you hit X", use MemoryHigh β but set it above the real anonymous working set, not at a number you wish the service used.
For a unit managed by systemd, do not edit files under /usr/lib/systemd/system. Use a drop-in:.
A few details that are easy to get wrong:.
The most common failure is setting MemoryMax to the RSS you see in ps on a quiet afternoon. That number excludes page cache, tmpfs, socket buffers, and the startup peak.
Recent findings suggest that example: a Python API with a 600 MB steady-state heap, a 900 MB startup spike, and 200 MB of page cache under load. MemoryMax=1.2G and MemoryHigh=1G gives the startup room, keeps the cache, and leaves the high limit above the anonymous working set. Setting MemoryHigh=700M because "700 MB sounded right" would reproduce the stall from the lab.
When something gets OOM-killed, these are the commands that answer "what happened":.
memory.reclaim is the manual version of the reclaim the kernel performs under pressure. On Linux 5.19+:.
It has been revealed that the kernel may reclaim more or less than requested; if it reclaims less, the write fails with EAGAIN. It is a nudge, not a guarantee, and the documentation is careful to say that proactive reclaim is not itself a memory-pressure signal.
It has been revealed that memory.events is hierarchical; memory.events.local counts only events at that cgroup level. If max is climbing but oom_kill is zero, the cgroup is touching the wall and recovering. If oom_kill is climbing, something is dying repeatedly, and a supervisor with Restart=always will turn that into a loop.
The kernel log line looks like this (values omitted because they depend on the process):.
The anon-rss and file-rss split tells you whether the victim was heap-heavy or cache-heavy. The PID tells you whether the killer picked the process you expected.
By default memory.oom.group is 0, so the OOM killer selects one task inside the cgroup, not the whole unit. That is why a multi-process service can lose one worker, restart it, and appear healthy while the leak continues. If you need all-or-nothing behavior, memory.oom.group=1 makes the cgroup an indivisible workload; tasks with oom_score_adj of -1000 are still exempt. Treat that as an advanced option, not a default.
systemd-oomd is a separate, optional service that watches PSI and can kill a whole cgroup before the kernel OOM path runs. It acts on units marked ManagedOOMMemoryPressure=kill and is configured through oomd.conf. It is a useful backstop on desktops and laptops; it is not a substitute for per-unit limits, and it is not installed everywhere.
MemoryHigh is a throttle, not a cap. MemoryMax is the cap, and it kills inside the cgroup instead of across the machine. Set MemoryMax from measured peaks with headroom, set MemoryHigh above the anonymous working set, and read memory.events when you want to know which one fired.
In a recent development, the OOM killer is not malicious. It is just the only thing left when nobody set a limit.