Memory cgroup out of memory: Killed process 20899 (python3) total-vm:158560kB, anon-rss:126080kB. The kernel logged that on a test container a fraction of a second after a third program asked for memory the group didn’t have. Process 20899 was not that program. It had been sitting quietly holding 120 MiB for several seconds, allocating nothing, and it died anyway because it was the biggest thing in the room. The program that actually ran the group out of memory kept running. That is the Linux out-of-memory killer working as designed, and the design is worth knowing before it picks the database.
What the OOM killer is for
The last resort when memory promised can’t be delivered
Linux hands out memory on credit. With the default heuristic overcommit, mode 0, the kernel’s overcommit documentation says only “Obvious overcommits of address space are refused.” A program can reserve far more than it touches, and most do. Trouble starts when enough of them try to use what they reserved at the same time. The kernel tries reclaiming page cache and swapping first. When nothing else frees enough, it picks a process and kills it, so the rest of the system can keep breathing.
Mode 2 is the strict alternative: total commitments are “not permitted to exceed swap + a configurable amount (default is 50%) of physical RAM.” That moves the failure from a kill to an allocation error inside the program that asked, which sounds better until a program that never checks for allocation errors gets one. The test container here ran mode 0, the default.
The OOM killer also works inside a memory-limited cgroup, which is how containers and systemd services get their own ceilings. The cgroup v2 documentation says of memory.max: “If a cgroup’s memory usage reaches this limit and can’t be reduced, the OOM killer is invoked in the cgroup.” Only processes inside the group are candidates. That made it safe to test: the experiments below ran inside a 200 MiB group on an Ubuntu 24.04 test container (kernel 6.18, 8 GB RAM, no swap), using the cgroup v1 memory controller this container exposes (memory.limit_in_bytes), so the rest of the machine was never at risk.

How it chooses: oom_badness in plain numbers
Biggest wins, with a dial for exceptions
The selection logic is a function called oom_badness in mm/oom_kill.c. Its comment states the goal plainly: “The goal is to return the highest value for the task consuming the most memory to avoid subsequent oom failures.” The score is resident memory plus swap entries plus page tables, counted in pages:
points = get_mm_rss_sum(p->mm) + get_mm_counter_sum(p->mm, MM_SWAPENTS) +
mm_pgtables_bytes(p->mm) / PAGE_SIZE;
Then the per-process adjustment, oom_score_adj, is scaled and added: adj *= totalpages / OOM_SCORE_ADJ_MAX; points += adj;. OOM_SCORE_ADJ_MAX is 1000 in include/uapi/linux/oom.h, so each point of adjustment is worth one thousandth of the memory being divided up. A task set to OOM_SCORE_ADJ_MIN, -1000, returns early and is never considered; the header’s comment says that value “disables oom killing for pid.”
The first test put three Python processes in the 200 MiB group: cache-A holding 40 MiB, big-B holding 120 MiB, then growing-C asking for 100 MiB more. The kernel’s task table from dmesg, memory in 4 KiB pages:
[ pid ] uid tgid total_vm rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name
[ 20855] 0 20855 19159 13100 11040 2060 0 176128 0 0 python3
[ 20899] 0 20899 39640 33587 31520 2067 0 331776 0 0 python3
[ 20948] 0 20948 16599 10448 8384 2064 0 151552 0 0 python3
Applying the formula to that table in Python gives cache-A 13,143 points, big-B 33,668 and growing-C 10,485. big-B was killed. growing-C, the process that tripped the limit, had the smallest score of the three at that instant because it had only reached about 41 MiB of its 100. It then finished allocating and printed “holding 100 MiB” with plenty of room, since its neighbor’s 120 MiB had just been freed.
A later run marked growing-C with oom_score_adj=1000 before it started. With the group’s 51,200-page limit as the scale, that adds 51 × 1000 = 51,000 points, far more than anything’s memory. growing-C was killed on its first OOM, and cache-A and big-B both survived.
Reading the dmesg report
The kill is one line; the evidence is the fifty above it
Every OOM kill leaves a block in the kernel log, readable with dmesg or journalctl -k. From the first test, with the CPU registers and stack trace trimmed:
python3 invoked oom-killer: gfp_mask=0xcc0(GFP_KERNEL), order=0, oom_score_adj=0
CPU: 1 UID: 0 PID: 20948 Comm: python3 Not tainted 6.18.44-fc-v880 #1 PREEMPT(none)
memory: usage 204800kB, limit 204800kB, failcnt 60
Memory cgroup stats for /oomtest:
Tasks state (memory values in pages):
oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0,oom_memcg=/oomtest,task_memcg=/oomtest,task=python3,pid=20899,uid=0
Memory cgroup out of memory: Killed process 20899 (python3) total-vm:158560kB, anon-rss:126080kB, file-rss:8268kB, shmem-rss:0kB, UID:0 pgtables:324kB oom_score_adj:0
Read from the top. The “invoked oom-killer” line names the process that was allocating when memory ran out, PID 20948 here, which is growing-C. The constraint= field says whether this was a whole-system OOM or a cgroup hitting its own limit; CONSTRAINT_MEMCG with oom_memcg=/oomtest means the 200 MiB group, not the machine. The memory: usage 204800kB, limit 204800kB line shows the group pinned at exactly its ceiling. The task table lists every candidate with its rss and oom_score_adj. The last line names the victim.
The two PIDs are different in this log, and that is the whole lesson of the first test. Searching logs for the name of the program that crashed finds the victim. The allocator that pushed things over the edge is in the first line, under “invoked oom-killer,” and it is often the one that needs fixing.
On a system with systemd, journalctl -k -g "Killed process" pulls out just the final lines across boots. On this container plain dmesg worked as root.

oom_score, oom_score_adj and systemd
The 666 floor and a dial that only turns one way
Each process exposes its current score in /proc/PID/oom_score. The values in the first test were odd at first glance:
pid=20855 cache-A oom_score=670 adj=0
pid=20899 big-B oom_score=677 adj=0
A 40 MiB process scoring 670 out of a nominal 1000 looks alarming. The display formula in fs/proc/base.c explains it: the code computes (OOM_SCORE_ADJ_MAX + badness * OOM_SCORE_ADJ_MAX / (long)totalpages) * 2 / 3, with totalpages being all RAM plus swap, and its comment says it scales the badness “into [0, 2000] range” before the two-thirds. Python reproduced both values from the dmesg table and the container’s 2,054,052 pages of RAM: (1000 + 6) × 2/3 = 670 and (1000 + 16) × 2/3 = 677. Any ordinary process with an adjustment of zero sits at 666 or a little above. Setting adj to 500 on the test shell moved its score to 1000, and 1000 moved it to 1334.
The adjustment range is -1000 to +1000, per the proc_pid_oom_score_adj man page, and “-1000, is equivalent to disabling OOM-killing entirely for that task.” On the test container, raising it worked for every account tried. Lowering it did not:
# root in this sandbox (CAP_SYS_RESOURCE dropped)
set 500: ok, oom_score now 1000
set -500: Permission denied (errno 13)
set 1000: ok, oom_score now 1334
# unprivileged user tester
set 300: ok, oom_score now 867
set 100: ok, oom_score now 734
set -100: Permission denied (errno 13)
The sandbox’s root account had CAP_SYS_RESOURCE removed, and it couldn’t go negative either; the man page ties that privilege to the older oom_adj file, and the test shows the same gate on oom_score_adj. The unprivileged user could lower its value back toward zero but not below it.
For services, systemd sets this at launch. systemd.exec describes OOMScoreAdjust= as setting “the adjustment value for the Linux kernel’s Out-Of-Memory (OOM) killer score for executed processes,” taking an integer from -1000 to 1000. OOMScoreAdjust=-900 on a database unit makes it a much worse target than a batch job without taking it off the list entirely.
Where people get burned
Old documentation, the wrong process, and a daemon that kills whole apps
The 3% root bonus that isn’t there. The proc man page still says “root processes are given 3% extra memory over other tasks.” A commit by David Rientjes, dated April 5, 2018, removed it: commit d46078b2 set out to “Remove the CAP_SYS_ADMIN bias so that all processes are treated equally,” and its message notes that the old credential lookup could dereference a potentially freed pointer for an exiting task. Current oom_badness has no root term. Advice built on the 3% bonus is about kernels from before that commit landed.
Blaming the victim. As the first test showed, the killed process is the biggest, not the guilty. A web app that leaks slowly can get a database killed every night. The “invoked oom-killer” line and a memory graph over time find the leaker. For a Python service, py-spy dump --pid from the Python profiling guide shows what the suspect is doing at that moment, though it measures CPU stacks, not memory.
Protecting everything. Setting -1000 on every important service leaves the OOM killer nothing it is allowed to kill. Memory still runs out, and the kernel has fewer choices.
systemd-oomd on desktops. systemd-oomd is a userspace daemon that “uses cgroups-v2 and pressure stall information (PSI)” and acts before the kernel does. It doesn’t pick single processes: it “will select a cgroup to terminate, and send SIGKILL to all processes in it.” Its oomd.conf defaults are SwapUsedLimit= 90%, DefaultMemoryPressureLimit= 60% and 30 seconds of pressure. When Ubuntu 22.04 shipped with it, a user filed a bug report that “firefox and visual studio code are frequently killed by systemd-oomd (every 2hours)” on an 8 GB machine. Canonical’s Nick Rosbrook wrote to ubuntu-devel in June 2022 that the swap limit “might be too easy to reach on Ubuntu, largely because Ubuntu provides just 1GB of swap.” A whole browser vanishing with no OOM block in dmesg points toward oomd rather than the kernel, since oomd kills from userspace; oomctl and journalctl -u systemd-oomd show what it saw. systemd’s own documentation says “It is highly recommended for the system to have swap enabled for systemd-oomd to function optimally.”

The case for letting it kill
Turning off the killer means trading a dead process for a dead machine
The strongest argument against all this tuning is that the OOM killer is usually right. Killing the largest process frees the most memory in one move and is the least likely to require a second kill, which is exactly what the kernel comment says it is aiming for. A system with every service protected and overcommit set loose tends to fail by thrashing instead, every process alive and none of them getting any work done. A clean kill and a restart from systemd is often the faster recovery.
The better fixes sit upstream of the killer: memory limits per service so one leak can’t take its neighbors, enough swap or compressed RAM to absorb spikes (the zram and zswap guide covers that), and explicit memory limits on the containers in any self-hosted Docker Compose stack, since the test above shows a limited group keeps the damage inside the group. memory.oom.group, which the cgroup v2 documentation says treats a cgroup “as an indivisible workload by the OOM killer,” keeps a multi-process app from being left half alive.
None of that changes the first test. A program that did nothing wrong held still, a neighbor overreached, and the quiet one paid for it. On your machines, which process is the biggest right now, and is it the one you would choose to lose?
Sources
- Linux kernel documentation (v6.6), Overcommit Accounting: https://www.kernel.org/doc/html/v6.6/mm/overcommit-accounting.html
- Linux kernel documentation (v6.6), Control Group v2: https://www.kernel.org/doc/html/v6.6/admin-guide/cgroup-v2.html
- Linux kernel source, mm/oom_kill.c: https://github.com/torvalds/linux/blob/master/mm/oom_kill.c
- Linux kernel source, include/uapi/linux/oom.h: https://github.com/torvalds/linux/blob/master/include/uapi/linux/oom.h
- Linux kernel source, fs/proc/base.c: https://github.com/torvalds/linux/blob/master/fs/proc/base.c
- proc_pid_oom_score_adj(5) man page: https://man7.org/linux/man-pages/man5/proc_pid_oom_score_adj.5.html
- systemd.exec(5), OOMScoreAdjust=: https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html
- Commit d46078b2, “mm, oom: remove 3% bonus for CAP_SYS_ADMIN processes”: https://android-kvm.googlesource.com/linux/+/d46078b2888947a86b6bb997cd5927e602e8fdc9
- systemd-oomd.service(8): https://www.freedesktop.org/software/systemd/man/latest/systemd-oomd.service.html
- oomd.conf(5): https://www.freedesktop.org/software/systemd/man/latest/oomd.conf.html
- Launchpad bug 1972159 message, foundations-bugs list: https://lists.ubuntu.com/archives/foundations-bugs/2022-June/477115.html
- Nick Rosbrook, “systemd-oomd issues on desktop,” ubuntu-devel: https://lists.ubuntu.com/archives/ubuntu-devel/2022-June/042116.html
