What happens when a Linux server runs out of memory?
As memory fills, the kernel first drops page cache, writes dirty data back to disk and, if swap exists, moves rarely used pages out. Those steps are cheap at first. When nothing more can be reclaimed and a program still needs a page, the kernel invokes the out-of-memory killer. It picks one process, kills it with SIGKILL and frees its memory so the system can continue. The killed program does not get a chance to clean up, so a database may need crash recovery and a web worker drops its request.
How does the OOM killer choose a victim?
Each process has an oom_score, mostly based on how much memory it uses, adjusted by oom_score_adj (from -1000 to 1000). The highest score is killed. That usually means the biggest memory user, which is often your database or main application, not the small runaway script that caused the shortage. You can see the current scores:
for p in /proc/[0-9]*; do printf '%5s %6s %s\n' "$(cat $p/oom_score 2>/dev/null)" "${p#/proc/}" "$(tr -d '\0' < $p/comm 2>/dev/null)"; done | sort -rn | head
Setting OOMScoreAdjust=-900 in a systemd unit makes that service much less likely to be chosen. Use it sparingly, for the one process that must survive, because the kernel still has to kill something.
How do I know an OOM kill happened?
journalctl -k -g 'Out of memory|oom-kill' --since yesterday dmesg -T | grep -i -A2 'killed process'
The log line names the process, its memory use and the constraint that triggered it. If it says CONSTRAINT_MEMCG, the limit was a cgroup limit set by systemd, Docker or Kubernetes, not the whole machine. A container killed with exit code 137 is the same event seen from the container side.
What causes OOM on servers?
- Too many workers. PHP-FPM
pm.max_children, Apache MaxRequestWorkers or app server threads set higher than RAM allows. - Database buffers sized for a bigger box. An
innodb_buffer_pool_sizeorshared_bufferscopied from another server. - Memory leaks. A process that grows steadily for days until something gives.
- Bursts. A batch job, a report or a backup compression run that briefly needs a lot.
- Container limits. A memory limit lower than the app's real peak.
How do I prevent it?
Start by sizing. Add up the expected peak of every service: database buffers, worker count times per-worker memory, caches and the OS itself, and keep about 20 percent free. Then add guard rails:
- Cap services with systemd:
MemoryHigh=throttles a service near its budget andMemoryMax=sets a hard limit, so one runaway service hits its own ceiling instead of the machine's. - Add a modest swap file as a buffer for rare peaks; Swap on Linux shows how. It turns a sudden kill into a slowdown you can notice.
- Run
systemd-oomd(enabled by default on Ubuntu) to act early, based on memory pressure, before the kernel killer fires. - Monitor available memory and alert below a threshold; Check memory usage covers the commands.
Is overcommit the problem?
Linux lets programs reserve more memory than exists, because most never touch all of it. That is overcommit, controlled by vm.overcommit_memory. The default heuristic mode works well for most servers. Some databases, such as Redis, recommend vm.overcommit_memory = 1 so background saves do not fail. Strict mode (2) prevents overcommit but makes allocation failures more common in ordinary programs. Changing it rarely fixes OOM; right-sizing does.
When is more RAM the answer?
When the working set really does not fit: a database whose hot data grew, a virtualization host with more guests, a cache that must hold more keys. Then a bigger machine is cheaper than constant tuning. Our models range from 16 GB DDR3 on E1 through 128 GB DDR4 on S5 up to 1 TB DDR5 on M13; the High memory line line exists for exactly this case. With a dedicated machine, the memory you see is all yours, so the numbers you measure are the numbers you get.
From 16 GB to 1 TB of RAM
| Server | Processor | Cores / threads | Memory | Storage | Network | Per month | Smallest order |
|---|---|---|---|---|---|---|---|
| E1 | Intel Xeon E3-1240 v3 | 4 / 8 | 16 GB DDR3 | 2x 240 GB SSD | 2x 1 Gbps | $30.52 | $91.56 (3 months) |
| S5 | Intel Xeon E-2276G | 6 / 12 | 128 GB DDR4 | 2x 1 TB NVMe | 2x 10 Gbps | $88.72 | $88.72 (1 month) |
| S7 | Intel Xeon E-2288G | 8 / 16 | 128 GB DDR4 | 2x 1 TB NVMe | 2x 10 Gbps | $100.72 | $100.72 (1 month) |
| M1 | 2x Intel Xeon Gold 5315Y | 16 / 32 | 512 GB DDR4 | 2x 4 TB NVMe | 2x 25 Gbps | $414.70 | $414.70 (1 month) |
| M13 | 2x Intel Xeon Gold 5418Y | 48 / 96 | 1 TB DDR5 | 2x 4 TB NVMe | 2x 25 Gbps | $849.12 | $849.12 (1 month) |
Keep Reading
Swap on Linux
What swap is for on a RAM-rich server, how to add a swap file on ext4 or XFS, zram as an alternative and how to tell useful swap from thrashing.
OpenCheck memory usage
Read free, /proc/meminfo, vmstat and per-process numbers correctly, and tell real memory pressure apart from a healthy page cache.
OpenLinux memory management
Virtual memory, pages, overcommit, huge pages, cgroup limits and the OOM killer: how Linux hands out RAM and the knobs worth turning.
OpenFAQ
Quick Answers
A part of the Linux kernel that kills a process when memory runs out, so the rest of the system can keep running.
Search the kernel log with journalctl -k -g oom-kill or dmesg -T and look for the Killed process line.
Lower its score with OOMScoreAdjust in the systemd unit. Another process will be chosen instead.
It gives a buffer for short peaks and turns sudden kills into slowdowns, but it does not fix a server that is simply too small.
The container was killed with SIGKILL, most often because it hit its memory limit.