Linux Internals

Linux Memory Overcommit and the OOM Killer Explained

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

You wake up to find MySQL or your Node.js app gone, and the logs say only "Killed". There was no crash trace and no error from the application itself. In most cases the culprit is the Linux out-of-memory killer, and the reason it had to act is memory overcommit: the kernel promised processes more memory than the machine actually had. This article explains how overcommit works, how the OOM killer chooses its victim and what you can do on a VPS to keep important services alive.

Why Linux promises more memory than it has

When a program calls malloc() or mmap() for a large block, the kernel usually does not hand over physical RAM immediately. It reserves address space and only backs a page with real memory when the program first writes to it. Many programs allocate far more than they ever touch: runtimes reserve large heaps, fork() duplicates a process's address space through copy-on-write, and thread stacks are sized generously.

If the kernel refused every allocation that could not be guaranteed up front, many normal workloads would fail early even though they would never use the memory. So by default Linux overcommits: it allows the total promised memory to exceed RAM plus swap, betting that not everyone will collect at once.

The three overcommit modes

The policy lives in vm.overcommit_memory:

sysctl vm.overcommit_memory vm.overcommit_ratio
ValueModeBehaviour
0Heuristic (default)Refuses only obviously impossible allocations
1AlwaysNever refuses; every allocation succeeds
2StrictCaps total commitments at a computed limit

In mode 2 the limit is swap plus vm.overcommit_ratio percent of RAM (50 by default), or vm.overcommit_kbytes if set. You can see the current state in /proc/meminfo:

grep -E 'CommitLimit|Committed_AS' /proc/meminfo

Committed_AS is how much memory has been promised. CommitLimit only enforces anything in mode 2. Seeing Committed_AS well above physical RAM in mode 0 is normal, not an emergency.

Strict mode turns surprise kills into allocation failures, which well-written software can handle. The cost is that applications may fail to start or fork even with free RAM available, so test carefully before using it in production. Redis, by contrast, recommends mode 1 so that background saves using fork() do not fail.

How the OOM killer chooses a victim

When the kernel truly runs out of memory it can reclaim, it must free some by killing a process. Each process has a score visible in /proc/<pid>/oom_score. The score is driven mainly by how much memory the process uses, adjusted by oom_score_adj, which ranges from -1000 to 1000.

for p in /proc/[0-9]*; do
  printf "%s %s %s\n" "$(cat $p/oom_score 2>/dev/null)" "${p#/proc/}" "$(cat $p/comm 2>/dev/null)"
done | sort -rn | head

The biggest memory user is usually chosen, which is why databases are frequent victims: they are often the largest process on the server.

Confirming an OOM kill

The kernel logs every kill:

sudo dmesg -T | grep -iE 'out of memory|killed process'
sudo journalctl -k | grep -i oom

You will see the killed process, its memory usage and a table of all processes at that moment. That table tells you what was actually consuming memory, which is often not the process that was killed.

Protecting critical services

The cleanest way to shape OOM behaviour is through systemd:

# sudo systemctl edit mysql
[Service]
OOMScoreAdjust=-500
MemoryMax=1500M

OOMScoreAdjust makes the service less attractive to the killer. MemoryMax puts it in a cgroup limit so that if it grows beyond that, it is contained rather than dragging the whole system down. Avoid setting -1000 on everything; if nothing can be killed, the system can hang instead.

For earlier, more predictable action, consider a userspace OOM daemon such as systemd-oomd (shipped with recent systemd) or earlyoom. They act on memory pressure before the kernel reaches its last resort, when the system is still responsive.

A practical checklist for a VPS

  1. Size your services: set buffer pools, worker counts and runtime heap limits so their sum fits RAM with headroom.
  2. Add a modest swap file as a buffer; see swappiness and swap on a VPS.
  3. Set OOMScoreAdjust for the services you most need to survive, and positive values for disposable workers.
  4. Use MemoryMax on services that can leak or spike.
  5. Monitor free -m, /proc/pressure/memory and the kernel log.
  6. If you are routinely near the limit, the honest fix is more RAM. Compare plan sizes on the pricing page.

FAQ

Should I set vm.overcommit_memory to 2 to stop the OOM killer?

It can reduce surprise kills, but programs will see allocation failures instead and many handle them poorly. Applications that fork large processes, like Redis saving to disk, may fail. Try it only on a test server with your real workload first.

Why was MySQL killed when PHP was using the memory?

The OOM killer picks the process with the highest score at that moment, which usually means the largest single process. Many small PHP workers together can exhaust RAM, yet the database is the biggest single target. Check the process table in the kernel log to see the real consumers.

Does adding swap prevent OOM kills?

It delays them and absorbs short spikes, but if memory demand keeps growing, swap fills and the OOM killer still acts. Swap is a cushion, not a replacement for enough RAM.

Related guides