Management Guides Troubleshooting
Linux OOM Killer on a VPS: Why Your Database Keeps Dying and How to Stop It
How to confirm that the Linux OOM killer stopped MySQL or another service, find what used the memory, and fix it: worker limits, database sizing, swap and systemd settings.
When a Linux server runs out of memory, the kernel picks a process and kills it so that the rest of the system survives. That mechanism is the OOM killer ("out of memory"). On a small VPS the victim is usually the database, because it is the largest process, and the visible result is a site that shows a database connection error at random times. This guide shows how to confirm that the OOM killer was responsible and how to stop it happening again.
Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.
Confirm it
sudo journalctl -k | grep -iE "out of memory|oom-kill|killed process"
sudo dmesg -T | grep -iE "out of memory|killed process"
A typical line names the victim and its memory use:
Out of memory: Killed process 1432 (mariadbd) total-vm:1893232kB, anon-rss:412280kB ...
If the service is managed by systemd, its status confirms it too:
systemctl status mariadb
# look for: Main process exited, code=killed, status=9/KILL
# or: Failed with result 'oom-kill'
No such lines means the crash has another cause. Check the service's own log instead.
Find out what used the memory
The process that was killed is not always the one that grew. The kernel log prints a table of all processes at the moment of the kill; the ones with the largest rss values are the consumers. For the current state:
free -h
ps aux --sort=-%mem | head -15
In free, the column to read is available. Memory shown as buff/cache is given back when programs need it and is not a problem.
Count workers as a group. Forty PHP processes at 60 MB each use more than the database does, though none of them appears at the top of the list alone:
ps -C php-fpm8.3 -o rss= | awk '{sum+=$1; n++} END {if (n) printf "%d processes, %.0f MB total, %.0f MB average\n", n, sum/1024, sum/n/1024}'
Replace php-fpm8.3 with the process name on your system (php-fpm on AlmaLinux and Rocky Linux; apache2 or httpd for Apache with mod_php).
Fix 1: size the workers to the RAM
This is the most common cause on web servers. The pool is allowed more workers than fit in memory, and a traffic spike or a crawler starts them all.
Work out the limit:
max workers = (total RAM - memory for database - memory for system and other services) / average worker size
On a 2 GB server with 600 MB set aside for the database and 400 MB for everything else, 1,000 MB remains. With workers averaging 60 MB that is 16, so set pm.max_children = 15 in the PHP-FPM pool file (/etc/php/8.3/fpm/pool.d/www.conf on Debian and Ubuntu, /etc/php-fpm.d/www.conf on AlmaLinux and Rocky Linux) and restart PHP-FPM. Use your own measured average from the command above; the figures here are only an illustration.
When all workers are busy, extra requests wait in a queue. That is slower for a moment and far better than a dead database.
Fix 2: size the database to the RAM
For MySQL and MariaDB the main setting is innodb_buffer_pool_size. Advice to set it to 70–80% of RAM assumes a machine that runs only the database. On a VPS that also runs the web server and PHP, a quarter to a third of RAM is a more realistic starting point. Each connection also uses memory, so keep max_connections near what you really use; the default of 151 is more than a small site needs.
For PostgreSQL the equivalent settings are shared_buffers and work_mem; the second is per operation and multiplies with concurrent queries.
Fix 3: add swap as a buffer
Many VPS images ship without swap, so the first memory spike goes straight to the OOM killer. A swap file of 1–2 GB gives the system room to slow down instead.
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/fstab
swapon --show
Swap is a safety margin. A server that uses it constantly is slow and still needs one of the other fixes. More detail: how to create a Linux swap file.
Fix 4: choose what gets killed and bring it back
You can tell the kernel to prefer other victims over the database, and tell systemd to restart a service that dies. Run sudo systemctl edit mariadb and add:
[Service]
OOMScoreAdjust=-600
Restart=on-failure
RestartSec=5s
A lower score (the range is -1000 to 1000) makes a process a less likely victim. This protects the database at the expense of something else, typically a web worker, which is cheap to lose. It does not create memory: if the server is too small, something will still be killed.
You can also cap a service so that it cannot take the whole machine. For a service that leaks memory, MemoryMax=600M in the same kind of override makes the kernel stop that service alone when it passes the limit.
Fix 5: find the leak or the spike
- It happens at the same time every day: a scheduled job. Check
crontab -l,/etc/cron.d/andsystemctl list-timers. Backup and import scripts are common offenders. - Memory climbs slowly for days: a leak in a long-running process. Restarting it on a schedule is a workaround until it is fixed.
- It follows traffic: check the access log for crawlers and for requests to heavy endpoints such as search,
xmlrpc.phpor the login page.
Record memory over time with sar -r from the sysstat package, so that the next incident comes with a history.
When to upgrade
If the workers and database are already sized as low as the site can tolerate and "available" memory is still near zero in normal operation, the server is too small. More RAM is then the correct fix, and usually the cheapest one measured against your time.
Frequently asked questions
Why does the OOM killer always choose MySQL?
It scores processes mainly by how much memory they hold, and the database is normally the largest single process. Lower its score with OOMScoreAdjust and reduce what the other processes use.
Can I disable the OOM killer?
Not usefully. Without it, a server that runs out of memory freezes instead of losing one process. Fix the memory shortage.
How much swap should a VPS have?
For 1–4 GB of RAM, 1–2 GB of swap is a common choice. It is there for spikes, not as extra working memory.
Does clearing the cache free memory for applications?
No. Cache is released automatically when applications need it. Dropping it by hand only makes the disk work harder afterwards.