The short answer
Confirm the OOM kills in the kernel log, find what uses the memory, cap the biggest users, add swap as a safety net and add RAM if the work really needs it.
Confirm it
sudo dmesg -T | grep -i -E "out of memory|killed process"
sudo journalctl -k | grep -i oomThe line names the process that was killed and how much memory it used. A service that restarts for no clear reason is often an OOM victim.
See who uses memory
free -h
ps aux --sort=-%mem | head -n 12Check the biggest users: database, PHP-FPM workers, Java apps, caches, Docker containers.
Common fixes
- PHP-FPM: lower
pm.max_childrenso the workers fit in RAM. Each worker can use tens or hundreds of MB. - MySQL or MariaDB: check
innodb_buffer_pool_sizeand the number of connections. - Caches and Redis: set a memory limit.
- One heavy job (a backup or an import): run it at a quiet time, with lower priority.
- Containers: set memory limits so one cannot take the whole server.
Add swap as a safety net
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileAdd it to /etc/fstab to keep it after a reboot. Swap is slow, so it prevents a crash but does not cure a real shortage.
When you simply need more RAM
If the memory is used for real work and the settings are sane, move to a bigger server. Check how memory use looks over a week, not one minute.
Watch it
Alert when free memory and swap stay low. A health agent can show memory over time and warn before the OOM killer acts.
Questions people ask
Is swap bad for a server?
A little swap is a good safety net. Constant heavy swapping means you need more RAM or a better setup.
Why does the OOM killer pick my database?
It picks the process with the highest score, usually the biggest one. You can protect a service with systemd settings, but fixing the memory use is better.
Do I need to reboot?
Not usually. Restart the killed service and fix the cause.