The OOM killer

Advanced · 1 min

Why Linux promises more memory than it has, how the OOM killer picks a victim, reading its kernel log report, and steering it with oom_score_adj and cgroups.

Before thisMemory, page cache and swap

Red Hat Enterprise Linux · 10 min · from 46:18

The OOM killer

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Red Hat Enterprise LinuxReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Linux overcommits: allocations succeed against memory that is only used when touched. The default heuristic mode refuses only obvious overcommits.
  • When memory really runs out and reclaim fails, the kernel kills a process instead of hanging. That is the OOM killer.
  • The victim is the process with the highest score: RAM, swap and page tables in use, as a share of the memory it could use, plus oom_score_adj.
  • oom_score_adj runs from -1000 to 1000. -1000 makes a process exempt. In a unit, set OOMScoreAdjust=.
  • The kernel log report has a header naming the allocating process, a table of candidates in pages, and a 'Killed process' line with the victim's memory in kB.
  • A cgroup that hits memory.max gets its own OOM kill inside that cgroup, logged as 'Memory cgroup out of memory'. The rest of the system is not touched.
  • systemd-oomd acts earlier, from memory-pressure readings, killing a whole cgroup before the kernel has to.

A bank that lends more than it holds

A process asks for 10 GB. Most programs ask for more than they use: a thread stack reserves megabytes and touches kilobytes, a forked process shares its parent’s pages until it writes to them. If the kernel refused every allocation it could not back with RAM right now, much of that memory would sit reserved and idle.

So Linux overcommits, the way a bank lends out more than it keeps in the vault, betting that not everyone withdraws at once. Most days that is right. When it is wrong, the kernel has two options: hang, or pick a process and kill it to get memory back. It kills. That is the OOM, out-of-memory, killer.

Allocation is not use

I ran this on this Mac, which has 16 GiB of RAM: map 64 GiB of anonymous memory, then touch the first 1 GiB of it, one byte per page. macOS overcommits too, so the idea shows, though the details of the policy differ from Linux:

mapped 64 GiB on a 16 GiB machine: ok
max RSS so far: 12 MiB
after touching 1 GiB, max RSS: 1036 MiB

The mapping succeeded and cost nothing. Resident memory grew only as pages were touched. A program that eventually touches more than the machine can supply fails at the touch, not at the allocation, and on Linux what fails is a page fault, which the OOM killer resolves by killing someone.

The three overcommit modes

The kernel’s overcommit-accounting document defines vm.overcommit_memory:

  • 0, heuristic (default): obvious overcommits of address space are refused, so a wild allocation fails, but normal overcommit is allowed.
  • 1, always: never refuse. For programs such as sparse-array scientific code.
  • 2, don’t overcommit: total committed memory may not exceed swap plus vm.overcommit_ratio percent of RAM (default 50). Allocations then fail with an error instead of a process being killed later.

/proc/meminfo shows Committed_AS, memory promised to processes, and CommitLimit, the ceiling that mode 2 enforces. Mode 2 sounds safer, but many programs are not written to handle allocation failure well, and it often refuses allocations that would have been fine. Most servers stay on 0 and control memory per service with cgroups.

How the victim is chosen

oom_badness() in mm/oom_kill.c is short enough to read. In the current kernel source the baseline score is the process’s resident pages, plus its swap entries, plus its page tables. The oom_score_adj value, scaled to the memory available in that context, is then added. The process with the highest result is killed. A scaled version of that score is shown in /proc/<pid>/oom_score: higher means more likely to be chosen. Compare processes on one machine with it rather than reading the number as a percentage.

Two things follow. The killer goes for the biggest process, which is often the one you care about most, such as the database. And it does not look at age, CPU or importance, only memory and your adjustment.

proc_pid_oom_score_adj(5) in man-pages 6.19 says root processes get “3% extra memory” in the calculation. The current kernel code above has no such bonus. Trust the code: older articles repeating the root bonus are describing older kernels.

Steering it

oom_score_adj ranges from -1000 to 1000. -1000 means never kill this process. Positive values make it a preferred victim.

cat /proc/4123/oom_score /proc/4123/oom_score_adj
choom -p 4123 -n 500        # util-linux helper to change it

For a service, set it in the unit. The Debian 13 systemd package I downloaded and unpacked on this Mac does exactly this for the journal:

# from systemd-journald.service
OOMScoreAdjust=-250

Logging is valuable during a memory crisis, so journald is made less likely to be chosen, without being exempt. Use -1000 sparingly. If every important process is exempt, the kernel kills whatever is left, and if nothing killable remains, the source shows it gives up with “Out of memory and no killable processes”.

Reading the report

When the killer runs, it writes a report to the kernel log, readable with journalctl -k or dmesg. I have no real report to show, because I have no Linux machine here. These are the format strings from mm/oom_kill.c, in the order they appear:

%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%d
Tasks state (memory values in pages):
[  pid  ]   uid  tgid total_vm      rss rss_anon rss_file rss_shmem pgtables_bytes swapents oom_score_adj name
oom-kill:constraint=%s,nodemask=%*pbl
%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%d

How to read one. The first line names the process that was allocating when memory ran out, which is often not the culprit, just unlucky. The task table lists every candidate. Note that its memory columns are in pages, usually 4 KiB, not kB. The constraint line says why: CONSTRAINT_NONE is the whole system, CONSTRAINT_MEMCG is a cgroup limit. The Killed process line names the victim and its memory in kB. Its prefix is Out of memory for a system-wide event or Memory cgroup out of memory for a cgroup one.

cgroups and systemd-oomd

Most OOM kills on modern servers are not system-wide. A cgroup with a memory limit, memory.max, set by MemoryMax= in a unit or by a container runtime, gets its own OOM kill when it hits the limit, per the kernel’s cgroup v2 documentation. The rest of the machine is not involved. Set memory.oom.group and all processes in the cgroup are killed together, so a half-dead service does not keep running. systemd’s OOMPolicy=kill sets that for you.

systemd-oomd is a userspace killer that watches memory pressure and swap use per cgroup and kills a whole cgroup earlier, before the kernel reaches the point of thrashing. Units opt in with ManagedOOMMemoryPressure= and ManagedOOMSwap=. The cgroups lesson covers the limits themselves.

About the video

The embed is a clip, 46:18 to 56:23, of the Red Hat Enterprise Linux channel’s livestream “Linux Memory (RAM) and Swap Management”, October 2024, in which Nate and Scott take questions on memory. The clip’s chapters are “overcommit_memory kernel tunable” and “oom_killer scoring and priority and tunables”. It is the overcommit and OOM part of a broader stream, so it lacks the log format, cgroup OOM and systemd-oomd, which the lesson adds. The full stream is in deeper on the previous lesson. English was judged from the channel, presenters and English auto-captions, the content from the chapters.

What was run: the overcommit demo on macOS and reading the Debian journald unit. The score calculation and log lines are read from mm/oom_kill.c in the Linux 7.4-rc4 tree. Modes and settings are from the kernel documentation, proc_pid_oom_score_adj(5) and the systemd 262 man pages, checked 2026-11-28.

Where this fits

Limiting a service’s memory, CPU and processes as a group, so one cannot starve the rest, is next: cgroups v2 and resource control.

Check yourself

  1. 01A Java service gets killed every few days, and `journalctl -k` shows 'Killed process 2231 (java)'. Where in the report do you see how much memory it was using?

    In the same line: anon-rss, file-rss and shmem-rss in kB. The task table above it lists every candidate's rss in pages, which shows what else was large at the time.

  2. 02Why does malloc of 10 GB succeed on a 4 GB machine, and the program die later?

    With overcommit, allocation reserves address space, not RAM. Pages are only backed when touched. When the program touches more than the machine can supply, the OOM killer runs then.

  3. 03You set oom_score_adj=-1000 on every important process. What can go wrong?

    Those processes can never be chosen, so the kernel kills whatever is left, or panics if nothing killable remains. Exempt only what must survive.

  4. 04A container with MemoryMax=512M is OOM-killed while the host has 20 GB free. Is the host short of memory?

    No. It is a cgroup OOM. The limit applies to that cgroup only, and the kernel logs it as 'Memory cgroup out of memory'.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark