What an out-of-memory kill looks like
When a Linux server runs out of RAM and swap, the kernel's OOM killer picks one process and ends it with SIGKILL. The program gets no chance to log anything, so from the outside it just vanishes. These are the signs:
- MySQL, PHP-FPM, Node or a container stops with no error in its own log, and systemd restarts it or leaves it dead.
- A service exits with code
137. That is 128 plus signal 9, which is SIGKILL. Other things can send SIGKILL too, so confirm in the kernel log. - Commands fail with
Cannot allocate memoryorFailed to fork. - The kernel log has lines such as
Out of memory: Killed process 2154 (mysqld)orinvoked oom-killer. - SSH hangs or times out while the server thrashes. If you cannot log in, reboot from the control panel console, then read the logs once you are back in.
Quick diagnosis
| What you see | Likely cause | First fix |
|---|---|---|
Killed process ... (mysqld) in the kernel log | MySQL holds the largest share of RAM, so it scores highest | Lower innodb_buffer_pool_size or cut PHP workers; add swap |
Killed process ... (php-fpm8.3) repeated | pm.max_children multiplied by worker size is larger than free RAM | Measure worker size, lower pm.max_children |
Kill during apt upgrade, npm install or a build | A short spike on a small plan with no swap | Add a swap file |
Memory cgroup out of memory | A container or systemd unit hit its own memory limit | Raise that limit or reduce what the app loads |
swapon --show prints nothing | No swap is configured | Create a swap file (steps below) |
Swap is full and si/so in vmstat stay above zero | The working set is larger than RAM | Tune the biggest process; if it persists, upgrade RAM |
| Kills continue after tuning and swap | The plan is too small for the workload | Move to more RAM |
| WordPress shows "Allowed memory size exhausted" | PHP's own memory_limit, not the server running out | See the WordPress memory guide linked below |
The last row is a different problem. PHP's memory_limit stops one script; the OOM killer stops a process on a server that has no memory left. Our guide to WordPress "Allowed memory size exhausted" covers the first.
Step 1: confirm the OOM kill in the kernel log
Read the logs before changing anything. Reading kernel messages needs sudo on Ubuntu, because dmesg can be restricted by the kernel's dmesg_restrict setting.
sudo dmesg -T | grep -i -E 'out of memory|killed process'
The -T flag prints readable times. The dmesg manual warns those times can be inaccurate after a suspend or resume. The kernel ring buffer is cleared on reboot, so if the server restarted after the kill, ask the journal instead:
# current boot
sudo journalctl -k -g 'out of memory|killed process'
# the boot before this one
sudo journalctl -k -b -1 -g 'out of memory|killed process'
-k limits the output to kernel messages and implies the current boot, which is why -b -1 is needed for the previous one. -g filters with a regular expression and is case-insensitive when the pattern is all lowercase. Older boots are only there if the journal is stored on disk. A kill looks like this (an example, with your own numbers):
Out of memory: Killed process 2154 (mysqld) total-vm:2412364kB, anon-rss:812340kB, file-rss:0kB, shmem-rss:0kB, UID:110 pgtables:2100kB oom_score_adj:0
Read it left to right:
- 2154 (mysqld) is the victim, by process ID and name.
- anon-rss is the memory the process held in RAM that cannot be dropped and re-read from a file. This is the number that got it chosen.
- oom_score_adj:0 means nobody had raised or lowered its chance of being killed.
A line such as php-fpm8.3 invoked oom-killer just above it names the process that asked for memory when none was left. That process is not necessarily the one that died. The kernel chooses the victim by score, so a small process can trigger the shortage while a large one is killed.
If your server also runs systemd-oomd, it can end whole services before the kernel does. It acts on memory pressure and works through cgroups. Check with systemctl status systemd-oomd and sudo journalctl -u systemd-oomd. If a container died, docker inspect -f '{{.State.OOMKilled}}' container_name prints true when the kernel killed it for exceeding its memory limit.
Step 2: find which process was killed and why
The kernel gives every process a badness score between 0 and 1000, shown in /proc/PID/oom_score. The proc manual says it is based mainly on memory use, and a higher score means the process is more likely to be selected. The value oom_score_adj is added to that score: it runs from -1000 to +1000 and you or a service manager can set it. At -1000 the process is never killed by the OOM killer.
See what is using memory right now, and which processes the kernel would pick next:
free -h
ps -eo pid,user,comm,rss --sort=-rss | head -10
for p in /proc/[0-9]*; do
printf '%s %s %s\n' "$(cat $p/oom_score 2>/dev/null)" "${p#/proc/}" "$(cat $p/comm 2>/dev/null)"
done | sort -rn | head -5
rss in the ps output is in kilobytes. The loop prints score, PID and name for the five most likely victims. To read one service, choom -p $(pidof -s mysqld) prints its score and its adjustment value. On a web server, check the database and the PHP workers first.
The main columns of free -h:
- available is the figure to watch. It estimates what programs can still get without swapping. Low free alone is normal, because Linux uses spare RAM as file cache.
- Swap used that keeps growing is a warning. A little swap in use after a spike is fine.
To see whether the server is swapping right now, run vmstat 1 5 and look at si and so, the amounts swapped in and out per second. Values that stay above zero mean active swapping. A short burst is normal; a steady stream means the working set does not fit.
Step 3: add a swap file on Ubuntu 24.04
Swap gives the kernel somewhere to move idle memory, so a spike produces a slowdown instead of a kill. It does not add working RAM: disk is far slower, and a server that swaps all day needs more memory. Our Ubuntu VPS initial setup guide has the short version of this step.
Check first
swapon --show
free -h
df -h /
findmnt -no FSTYPE /
If swapon --show lists something, such as /swap.img from the Ubuntu installer, swap already exists. Decide whether it is big enough before adding a second file. The swap file takes its full size from the disk straight away, so check that df -h / shows room. If the disk is nearly full, see No space left on device first. The last command prints the filesystem type: ext4 works with the steps below.
Create, secure and enable it
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
fallocate -l 2Greserves a 2 GB file. For sizing, 2 GB suits a 1 to 2 GB plan, which matches our VPS RAM sizing guide. For larger plans, 2 to 4 GB is a practical ceiling.chmod 600lets only root read it. Swap can hold copies of whatever was in memory, including passwords and keys.mkswapwrites the swap signature.swaponstarts using it immediately.
The swapon manual says swap files with holes are rejected, and warns that preallocated files may count as having holes on some filesystems. It names dd as the most portable way to create one. If swapon reports "swapfile has holes", or the root filesystem is XFS or btrfs, make the file with dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Then run the same chmod, mkswap and swapon lines. On btrfs a swap file also needs the nocow attribute, so follow the btrfs documentation there.
Make it survive a reboot
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
Verify
swapon --show
free -h
sudo swapoff /swapfile && sudo swapon -a
swapon --show
swapon --show should list /swapfile with size 2G. The swapoff and swapon -a pair proves the fstab line works without a reboot. Do not run swapoff while the server is already short of memory, because it needs room to move the swapped pages back into RAM.
Remove it later
sudo swapoff /swapfile
sudo nano /etc/fstab # delete the /swapfile line
sudo rm /swapfile
Step 4: set swappiness
vm.swappiness tells the kernel how willing it is to use swap compared with dropping file cache. The kernel documentation gives a range of 0 to 200 and a default of 60. Lower values make swap more expensive in the kernel's eyes. A value of 0 does not turn swap off: it means swapping starts only when free memory and file-backed pages are both below the zone's high watermark.
The default of 60 is a sensible starting point. If you want the kernel to keep application memory in RAM longer, for example on a database server, set a lower value such as 10 and watch the effect:
cat /proc/sys/vm/swappiness
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
cat /proc/sys/vm/swappiness
A file in /etc/sysctl.d/ is read again at boot, so the value stays. Change one thing at a time and compare vmstat 1 5 before and after.
Step 5: reduce what is using the memory
Swap postpones the kill. The lasting fix is to make the biggest consumer fit.
PHP-FPM: size pm.max_children to your RAM
Every PHP-FPM worker is a full process. If a pool allows more workers than RAM can hold, a traffic spike pushes the server into the OOM killer. The PHP manual defines pm.max_children as the maximum number of workers when pm is dynamic or ondemand. Measure the average worker first. On Ubuntu 24.04 the PHP version is 8.3 and the process is php-fpm8.3:
ps --no-headers -o rss -C php-fpm8.3 | awk '{s+=$1; n++} END {printf "%d processes, average %.0f MB\n", n, s/n/1024}'
The figure counts shared pages in every worker, so it slightly overstates real use. That errs on the safe side. Then work out the budget:
pm.max_children = RAM you can give PHP / average worker size
# example: 2048 MB / 60 MB = 34, so set 30
The example numbers are illustrative. Leave RAM for the operating system, MySQL and the web server first. Edit /etc/php/8.3/fpm/pool.d/www.conf:
pm = ondemand
pm.max_children = 30
pm.process_idle_timeout = 10s
pm.max_requests = 500
ondemandstarts workers only when requests arrive and ends idle ones afterpm.process_idle_timeout(default 10s). It keeps idle memory low on a quiet site.dynamickeeps a set of spare workers ready.pm.max_requestsrecycles each worker after that many requests. The PHP manual says this can work around memory leaks in third-party libraries. The default of 0 never recycles.
Test and reload:
sudo php-fpm8.3 -t
sudo systemctl reload php8.3-fpm
MySQL and MariaDB
The InnoDB buffer pool is the biggest fixed allocation. The MySQL manual says InnoDB allocates it at startup, a recommended value is 50 to 75 percent of system memory, and a pool that is too large may cause swapping. On a server that also runs PHP and the web server, keep it well under that range. You can change it without a restart:
SELECT @@innodb_buffer_pool_size/1024/1024 AS mb;
SET GLOBAL innodb_buffer_pool_size = 536870912; -- 512 MB
Put the same value in /etc/mysql/mysql.conf.d/mysqld.cnf under [mysqld] so it survives a restart. Each connection also takes its own thread memory, and sorts and table scans allocate extra buffers per request, so a very high max_connections can raise peak use. Our slow MySQL queries guide shows how to check whether the pool is too small before you shrink it.
Containers and other services
- Docker: set a memory limit per container so one app cannot take the whole server, for example
docker run --memory=1g. The kill then stays inside that container. - systemd services:
sudo systemctl edit servicenameand add[Service]withMemoryMax=1Gto cap one unit. - Node, Java and Python apps: reduce the worker count or heap size. A heap set equal to total RAM leaves nothing for the kernel and ends in a kill.
Protect one important service
You can lower a service's chance of being picked. In systemd, OOMScoreAdjust= accepts -1000 to 1000, and a lower value protects the unit. Check the current value first, because a package may already set one:
cat /proc/$(pidof -s mysqld)/oom_score_adj
sudo systemctl edit mysql
In the editor that opens, add:
[Service]
OOMScoreAdjust=-500
Save, run sudo systemctl restart mysql, and read oom_score_adj again to confirm. On MariaDB the unit is mariadb and the process is mariadbd. The kernel still has to free memory, so it kills something else instead. Use a moderate value, never -1000 for a large service, and fix the memory problem as well.
When to upgrade RAM instead
Tuning and swap are enough when the problem is a spike or a misconfigured pool. Move to a bigger plan when:
- kills or swap-heavy slowdowns return after you have sized PHP-FPM and MySQL;
vmstat 1 5showssiandsoabove zero during normal traffic, not only during a backup or an update;availableinfree -his close to zero at normal load;cat /proc/pressure/memoryshows asomeline withavg60above zero for long stretches (pressure stall information, which kernels with PSI enabled provide);- you have added a database, a cache or more sites, and the sum of their peaks is bigger than the plan.
Our guide to how much RAM a VPS needs gives sizes by workload. A Bigdomainhost KVM VPS can be resized in place with one reboot, so you do not need to rebuild the server to add memory.
Common mistakes
- Treating swap as extra RAM. It buys time. A server that swaps constantly is slow, and still gets killed once swap fills.
- Setting
vm.swappiness=0to switch swap off. It lowers the kernel's use of swap and does not disable it. Remove swap withswapoffand the fstab line. - Making the swap file with
cportruncate. The swapon manual says files with holes are rejected. - Skipping
chmod 600. A world-readable swap file can expose memory contents. - Forgetting the fstab line. Swap then disappears at the next reboot and the kills return.
- Raising
pm.max_childrento fix 502 errors. More workers use more memory. Count what fits. - Setting
OOMScoreAdjust=-1000on a big service. The kernel then ends other services, or the server stops responding. - Changing
vm.overcommit_memoryorvm.panic_on_oomwithout testing. The kernel documentation sayspanic_on_oom=1makes the kernel panic on an out-of-memory condition, and mode 2 ofovercommit_memoryapplies a never-overcommit policy, so applications can fail in new ways. - Restarting the killed service and stopping there. A restart clears the symptom. Find the process that grew.
- Putting swap on network storage. A swap file belongs on the server's own disk.
On Bigdomainhost servers
On a Bigdomainhost KVM VPS you have root access, so every command above works as written. Each VPS runs its own kernel, so free, dmesg and the OOM killer report on your server's memory alone. If SSH stops responding during a memory shortage, use the out-of-band console in the control panel to reboot, then read the kernel log. Need a second pair of eyes on a log? Message us on WhatsApp at +91 75994 50220 at any time, call during IST working hours, or email support@bigdomainhost.com.
This guide was written by the Bigdomainhost team with AI assistance. Commands and behaviour were checked against the Linux kernel documentation, Linux man pages, systemd, PHP and MySQL 8.0 documentation on 29 November 2026. The commands were not run on a test server for this guide, so try them on a non-production machine first.
Sources (checked )
- Linux kernel documentation: Documentation for /proc/sys/vm (swappiness, overcommit_memory, panic_on_oom)
- Linux man-pages: proc_pid_oom_score_adj(5): adjusting the OOM-killer score, range -1000 to +1000
- Linux man-pages: proc_pid_oom_score(5): the score used for OOM-killer selection
- Linux man-pages: swapon(8): swap files, holes, dd and priority
- Linux man-pages: journalctl(1): --dmesg, --grep, --boot, --since
- Linux man-pages: dmesg(1): --ctime and dmesg_restrict
- systemd documentation: systemd.exec(5): OOMScoreAdjust=
- systemd documentation: systemd-oomd.service(8): userspace out-of-memory killer
- PHP Manual: FastCGI Process Manager (FPM) configuration: pm, pm.max_children, pm.max_requests
- MySQL 8.0 Reference Manual: How MySQL Uses Memory
- MySQL 8.0 Reference Manual: Configuring InnoDB Buffer Pool Size (dynamic resizing)