Out of Memory (OOM Killer)
Linux Linux
Severity: CriticalWhat Does This Error Mean?
When Linux runs out of RAM, the Out of Memory Killer (OOM Killer) steps in and terminates one or more processes to free up memory. You will see messages in the system log like 'Out of memory: Kill process [PID]' followed by the name of the killed program. This is a safety mechanism — without it, the entire system would freeze.
Affected Models
- Ubuntu
- Debian
- Fedora
- CentOS
- Arch Linux
- Linux Mint
- openSUSE
Common Causes
- The system does not have enough physical RAM for all the running programs
- A program has a memory leak and is consuming more and more RAM until the system runs out
- Swap space (disk-based virtual memory) is disabled, too small, or exhausted
- A database, web server, or development tool is configured to use more memory than is available
- Multiple large programs are running simultaneously on a low-memory system
How to Fix It
-
Check which process was killed. Run: sudo journalctl -k | grep -i 'killed process' or: sudo dmesg | grep -i 'oom' to see what was killed and why.
The OOM Killer logs every process it kills along with the memory usage at the time. This identifies which program is causing the memory pressure.
-
Check current memory usage. Run: free -h to see total, used, and available RAM. Run: ps aux --sort=-%mem | head -20 to see the top 20 memory-consuming processes.
If a single process is using an unusual amount of RAM (such as a web browser using 8 GB), that process is the likely cause.
-
Add or increase swap space. Check current swap: swapon --show. Create a 2 GB swap file: sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
Swap is disk space used as extra RAM when physical RAM is full. It is slower than RAM but prevents OOM kills. For a server, recommended swap is 1 to 2 times the amount of RAM.
-
Find and fix memory leaks. Monitor memory usage of a specific process over time: watch -n 5 'ps -o pid,vsz,rss,comm -p [PID]'. If the memory keeps growing without stopping, the process has a memory leak.
Restart the leaking service to free memory temporarily. Look for updates from the software developer that fix the leak.
-
Limit process memory with cgroups or ulimit. Set a memory limit for a user: sudo ulimit -v [kilobytes]. For a service, add MemoryLimit=2G in its systemd unit file under [Service].
Limiting memory prevents one process from consuming all RAM. The process is killed by the limit rather than the OOM Killer taking down something more critical.
When to Call a Professional
OOM events are manageable with system configuration. For servers experiencing frequent OOM kills, a system administrator can tune memory settings, configure swap, and identify memory-leaking processes. For home use, adding more RAM or swap is usually the simplest solution.
Frequently Asked Questions
Should I always have swap enabled on Linux?
Generally yes — even on systems with plenty of RAM. Swap provides a safety net when memory usage spikes unexpectedly. On SSDs, swap causes minimal performance penalty and can be the difference between a smooth crash and the OOM Killer terminating your work. For servers, having at least some swap is strongly recommended.
What is the difference between swap partition and swap file?
A swap partition is dedicated disk space set aside at installation time for swap use. A swap file is a regular file on your existing file system that Linux uses as swap. Both work equally well on modern Linux. Swap files are more flexible because you can add, resize, or remove them without repartitioning.
The OOM Killer killed my database server. How do I prevent this?
First, make sure you have adequate swap (at least as much as your database's expected working set). Then configure the OOM score for your database service: echo -1000 > /proc/[pid]/oom_score_adj to make Linux less likely to kill it. You can also set this in the systemd unit file with: OOMScoreAdjust=-1000