Linux / OS Fundamentals Series

💀 A Top 1% Hands-On for Triggering Linux's OOM Killer Yourself and Pinpointing the Cause From dmesg and cgroup v2's memory.events — The Process Wasn't "Crashing," the Kernel Was "Killing" It

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 19.2 in the Linux/OS Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

A process that exceeds a memory limit doesn’t exit abnormally on its own. The kernel’s OOM Killer detects a system-wide memory shortage (or a cgroup’s memory limit being exceeded), judges “which process should be terminated” based on a score, and forcibly terminates it with SIGKILL. When a container suddenly restarts with exit code 137 (128 + 9, meaning termination by SIGKILL), that container wasn’t crashing from its own bug — the OOM Killer “killed” it — that’s the real identity of this phenomenon.

graph TB
    Process["A process that keeps using memory"] --> Limit{"Has it hit<br/>the memory limit?"}
    Limit -->|"No"| Process
    Limit -->|"Yes"| OOM["The kernel's OOM Killer activates"]
    OOM --> Score["Compares oom_score across every process<br/>(higher memory-usage ratio = higher score)"]
    Score --> Kill["Sends SIGKILL to the highest-scoring process"]
    Kill --> Log["Recorded in dmesg and memory.events"]

Hands-On: Actually Trigger the OOM Killer and Pinpoint the Cause

Step 1: Prepare a cgroup With a Memory Limit

Using the same approach as the cgroups hands-on, create a cgroup with memory.max set to a small value (e.g., 100MB).

sudo mkdir /sys/fs/cgroup/oom-test
echo "104857600" | sudo tee /sys/fs/cgroup/oom-test/memory.max

Step 2: Run a Process That Deliberately Devours Memory, Inside That cgroup

Prepare a simple script, in Python or similar, that just keeps growing its allocated memory without ever freeing it, register its PID into the cgroup’s cgroup.procs, then run it.

data = []
while True:
    data.append(bytearray(10 * 1024 * 1024))  # Allocate 10MB at a time

Step 3: Confirm the OOM Killer Fired, via dmesg

Once the memory limit is hit, the process suddenly terminates. Checking dmesg shows a line reading “Out of memory: Killed process,” along with which process (PID, process name) was chosen, and in which cgroup it happened.

Step 4: Confirm It From the cgroup Side Too, via memory.events

cat /sys/fs/cgroup/oom-test/memory.events

The oom and oom_kill counters in here show how many times the OOM Killer actually fired for this cgroup. Checking both dmesg (a system-wide log) and memory.events (a per-cgroup record) lets you confidently pinpoint: “it was this cgroup’s memory limit that caused it.”

Which Process Gets Chosen Is Decided by a Metric Called oom_score

The OOM Killer compares each process’s oom_score (ranging 0 to 1000) and picks the highest-scoring one to terminate. This score is calculated based on what fraction of the allowed memory that process is using — and when a cgroup’s memory limit is set, the baseline isn’t the system’s total memory, but that cgroup’s own usage ratio against its memory.max. You can also adjust this by changing /proc/<pid>/oom_score_adj (ranging -1000 to 1000) — “prioritize killing” a specific process, or make it “never get killed” (by setting -1000).

What a Pro Sees Here (Top 1% Understanding)

The OOM Killer Isn’t a Bug — It’s a Deliberate Design to Prevent the Whole System From Locking Up

Leaving a genuinely exhausted memory state unaddressed either sends the whole system into thrashing — stuck doing nothing but swap reads and writes — or makes it hang completely. The OOM Killer is a deliberate design, sacrificing one process to protect the whole system’s responsiveness, to avoid that worst-case scenario. During an incident investigation, when you find yourself thinking “I don’t know why this process died,” checking dmesg first, not just that process’s own logs or stack trace, is the first move a top-1% engineer makes.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. A process suddenly terminated, leaving no log at all: First check dmesg for a line reading “Out of memory.”
  2. A container keeps restarting with exit code 137: Check the container’s memory limit (memory.max) and the oom_kill counter in memory.events.
  3. You never want one specific critical process to ever be an OOM Killer target: Consider setting that process’s /proc/<pid>/oom_score_adj to -1000.

Summary

Takeaways to Apply Today

  1. When a process terminates for an unknown reason, make it a habit to check dmesg first.
  2. When a container’s exit code is 137, put the memory limit and the OOM Killer at the top of your list of investigation candidates.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).