When Linux runs out of RAM and swap, the kernel’s OOM killer ends the process with the highest oom_score to free memory. Confirm it in the kernel log with journalctl -k | grep -i "killed process", then find what consumed the memory: usually a leaking application, a database sized for a bigger server, or too many worker processes.
Symptoms
- A service stops suddenly with no application error
- systemd shows a unit failed with result oom-kill
- Kernel log lines like “Out of memory: Killed process 1234 (mysqld)”
How to diagnose it
Confirm an OOM kill happened
The log names the killed process and its memory use at the time.
journalctl -k | grep -iE "out of memory|killed process|oom" sudo dmesg -T | grep -i oomCheck current memory
Look at the available column, not free — Linux uses spare RAM for cache and releases it on demand.
free -mFind the largest processes
RSS is resident memory in kilobytes.
ps -eo pid,user,rss,%mem,cmd --sort=-rss | head -15Check cgroup limits
Services and containers can be OOM-killed at their own limit even when the host has free memory.
systemctl show SERVICE -p MemoryMax -p MemoryCurrent docker stats --no-stream
Common causes and fixes
Memory leak in an application
Memory that grows steadily until a crash is a leak. Restart on a schedule as a stopgap and profile the application to fix it.
Database configured for more RAM than the server has
Reduce innodb_buffer_pool_size (MySQL) or shared_buffers (PostgreSQL) to fit, leaving room for the OS and other services.
Too many workers
Each PHP-FPM, Gunicorn or Puma worker uses memory. Set the worker count from average worker RSS and available RAM.
No swap on a small server
A small swap file gives the kernel room to absorb short spikes. It will not fix a leak.
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstabContainer memory limit too low
Raise the limit (docker run --memory, Kubernetes resources.limits.memory) or reduce the application’s memory use.
Prevent it happening again
- Alert when MEM_used is above 90% for 5 minutes.
- Alert on SWAP_used climbing — swap filling while available memory hits zero is the usual warning before an OOM kill.
- Restart critical services automatically with Restart=on-failure in their systemd unit.
Set it up: Linux monitoring · Docker monitoring.
FAQ
How do I know if the OOM killer killed my process?
Search the kernel log: journalctl -k | grep -i "killed process". systemd also records Result: oom-kill for the unit.
Can I protect a process from the OOM killer?
Yes, set OOMScoreAdjust=-1000 in its systemd unit or write to /proc/PID/oom_score_adj. Use sparingly: the kernel will kill something else instead.
Why does Linux show almost no free memory?
Linux uses unused RAM as file cache. The available figure in free -m is what applications can actually use.
Related guides
Get alerted before this becomes an outage.
Install the agent in one line. Statance watches CPU, memory, disk and services, and its AI assistant helps find the cause.