Diagnosing 'Killed' Processes: Linux OOM Killer & Swap
When your backend process vanishes into thin air with a blunt 'Killed', don't blame cosmic rays. Meet the kernel Out-Of-Memory Killer, discover the autopsy in dmesg, and build real defenses.
You will learn why processes abruptly die with nothing more than the single word 'Killed', how to locate the execution record in kernel logs using dmesg, and how to prevent rogue workloads from starving your entire system using swapfiles and systemd memory limits.
1. The big idea: The ruthless Grim Reaper in ring 0
There is a special circle of engineering hell reserved for Friday afternoons at 4:45 PM. You launch a critical data ingestion script, your terminal hangs for thirty seconds, the shell prompt reappears, and mockingly prints exactly one word:
KilledNo stack trace. No exit handler output. No graceful error logged to your application file. Your process did not crash due to bad code syntax—it was brutally assassinated.
To understand why, you must understand the optimistic lie at the core of modern operating systems: memory overcommit.
The Linux kernel acts like an unscrupulous airline. If an airline has 100 seats on a plane, it sells 120 tickets, banking on the statistical likelihood that a few passengers will miss their flight or cancel. Similarly, Linux routinely promises running programs far more virtual RAM (Random Access Memory) than physical RAM exists in the server hardware. Programs allocate memory greedily using system calls like brk or mmap, but they do not touch every byte immediately.
When every passenger actually shows up at the gate—or when your Python data pipeline attempts to materialize a 16 GB dataset on an 8 GB virtual machine—the kernel runs completely out of physical pages. It cannot fulfill its promises.
To prevent the entire machine from locking up in a kernel panic, Linux invokes the OOM Killer (Out-Of-Memory Killer). The kernel calculates an internal badness score (oom_score) for every running process, selects the biggest, most offensive offender, and sends it a non-maskable SIGKILL (signal 9). SIGKILL cannot be caught, handled, or ignored. The process simply ceases to exist.
2. Reading the autopsy: Hunting in dmesg
Because SIGKILL strips your process of any chance to log its own demise, you must look outside user-space and consult the Linux kernel's ring buffer using dmesg (display message).
Let us run a targeted query. We use the -T flag to display human-readable timestamps (instead of confusing uptime seconds) and search case-insensitively for memory eviction events:
sudo dmesg -T | grep -i -E "oom[- ]killer|out of memory|killed process"If the OOM killer was active, you will see output that looks strikingly like this:
[Fri Nov 23 16:47:12 2026] node invoked oom-killer: gfp_mask=0x1100cca(GFP_HIGHUSER_MOVABLE), order=0, oom_score_adj=0
[Fri Nov 23 16:47:12 2026] CPU: 1 PID: 2841 Comm: node Not tainted 6.8.0-48-generic #48-Ubuntu
[Fri Nov 23 16:47:12 2026] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009)
...
[Fri Nov 23 16:47:13 2026] Tasks state (memory values in pages):
[Fri Nov 23 16:47:13 2026] [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name
[Fri Nov 23 16:47:13 2026] [ 2841] 1000 2841 1421008 952104 8146944 0 0 node
[Fri Nov 23 16:47:13 2026] Out of memory: Killed process 2841 (node) total-vm:5684032kB, anon-rss:3808416kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:7956kB oom_score_adj:0Let us translate the kernel's cold ledger into plain English:
total-vm: The total virtual memory allocated by the program (here ~5.6 GB).anon-rss(Resident Set Size): The actual physical RAM currently holding application heap and data pages (here ~3.8 GB).oom_score_adj: The score adjustment factor (ranging from -1000 for "never kill me" to 1000 for "kill me first").Killed process 2841 (node): The verdict and execution confirmed.
You can also check the current OOM score of any active process at any time by inspecting /proc:
# Inspect the badness score of your process (e.g., PID 2841)
cat /proc/2841/oom_scoreOOM Incident Resolution WorkflowClick to expandClick to collapse
Process exits with code 137 or raw Killed status.
Run dmesg -T | grep -i oom to identify victim PID and memory usage.
Create and activate a swapfile to provide memory headroom.
Configure systemd MemoryMax or container limits to isolate services.
3. Immediate breathing room: Creating a swapfile
Many modern cloud VMs (Virtual Machines) ship with precisely zero bytes of swap space. When physical RAM is exhausted, there is no emergency pressure release valve; eviction happens instantly.
Swap is space on a secondary storage drive (like an SSD or NVMe) used as overflow when RAM fills up. While reading and writing to disk is significantly slower than RAM, having swap prevents instantaneous execution: it turns a catastrophic hard crash into manageable disk thrashing, giving you time to respond.
Let us provision a 4 GB swapfile properly using standard Linux utilities:
# 1. Allocate a 4GB file with fallocate
sudo fallocate -l 4G /swapfile
# 2. Secure file permissions (vital: swap contains raw memory dumps!)
sudo chmod 600 /swapfile
# 3. Format the file as Linux swap space
sudo mkswap /swapfile
# 4. Activate the swap partition immediately
sudo swapon /swapfile
# 5. Verify the kernel recognizes it
free -hYour output will reflect the new safety net:
total used free shared buff/cache available
Mem: 3.8Gi 1.2Gi 1.8Gi 12Mi 850Mi 2.4Gi
Swap: 4.0Gi 0B 4.0GiTo make this swap permanent across server reboots, append the mount entry to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab4. Long-term defense: systemd isolation and cgroups
Swap gives you headroom, but a runaway memory leak will eventually devour 4 GB of swap, 8 GB of swap, and everything else in sight. The ultimate architectural fix is cgroups (control groups), which fence off processes so one runaway service cannot murder its neighbors.
If you run your services via systemd, you can enforce hard and soft memory limits directly in the unit file without touching complex kernel parameters.
Edit your service definition:
sudo systemctl edit worker.serviceAdd resource controls under the [Service] directive:
[Service]
# Soft throttle: systemd starts aggressively reclaiming cache at 1.5G
MemoryHigh=1.5G
# Hard ceiling: if it touches 2.0G, ONLY this service is killed
MemoryMax=2.0G
# Prevent this service from crashing the whole machine
MemoryAccounting=trueSave the file and reload the systemd manager configuration:
sudo systemctl daemon-reload
sudo systemctl restart worker.serviceNow, if your worker hits a memory leak, the kernel cgroup controller will terminate only that isolated unit instead of randomly sacrificing your database or your SSH daemon (sshd).
Run systemctl show worker.service -p MemoryCurrent,MemoryMax to verify that systemd is actively accounting and enforcing constraints on your process.
Common pitfalls
| Mistake | Why it hurts | Fix |
|---|---|---|
Setting chmod 777 on a swapfile | Any unprivileged user or malicious process on the box can read raw memory contents (including passwords and private keys) | Always enforce chmod 600 before running mkswap |
| Relying entirely on exit codes | SIGKILL exits with code 137 (128 + 9), but manual kill -9 yields the identical code | Always verify via dmesg -T before assuming memory exhaustion |
Disabling overcommit (vm.overcommit_memory = 2) | Massive applications like Redis or JVMs fork child processes that rely on Copy-On-Write; strict no-overcommit causes valid allocations to fail prematurely | Keep overcommit enabled, configure proper swap, and clamp processes via cgroups |
Ignoring systemd-oomd | Modern distros (Ubuntu 22.04+, Fedora) run a user-space OOM daemon that kills slices before the kernel OOM killer fires, leaving zero dmesg logs | Check journalctl -u systemd-oomd if dmesg shows no events |
What you learned
- The kernel overcommits virtual memory; when reality catches up with physical limits, the OOM killer executes the highest-scoring offender via uncatchable
SIGKILL. - A clean exit code 137 is ambiguous; the authoritative source of truth for an OOM termination is found in
dmesg -T. - A properly secured (
chmod 600) swapfile acts as an essential buffer against instantaneous application crashes. - Restricting workloads with
systemdcgroup limits (MemoryMax) guarantees that memory leaks remain contained to the offending service instead of threatening the host system.