Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

VPS out of memory: read the OOM killer log, add swap and stop it happening again

A service disappears, a site shows 502, and nothing in the app's own log explains it. On a Linux VPS the cause can be the OOM killer. This guide shows how to confirm the kill in dmesg or journalctl, find which process died and why, add a swap file on Ubuntu 24.04, tune swappiness, size PHP-FPM and MySQL, and decide when more RAM is the real answer.

Short answer: Confirm the kill with sudo dmesg -T | grep -i -E 'out of memory|killed process', find the biggest memory users with ps, then add a 2 GB swap file (fallocate, chmod 600, mkswap, swapon, fstab) and fix the process that grew, such as PHP-FPM pm.max_children or the MySQL buffer pool. If kills return, the plan needs more RAM.

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 memory or Failed to fork.
  • The kernel log has lines such as Out of memory: Killed process 2154 (mysqld) or invoked 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

VPS out of memory: symptom, likely cause and first fix
What you seeLikely causeFirst fix
Killed process ... (mysqld) in the kernel logMySQL holds the largest share of RAM, so it scores highestLower innodb_buffer_pool_size or cut PHP workers; add swap
Killed process ... (php-fpm8.3) repeatedpm.max_children multiplied by worker size is larger than free RAMMeasure worker size, lower pm.max_children
Kill during apt upgrade, npm install or a buildA short spike on a small plan with no swapAdd a swap file
Memory cgroup out of memoryA container or systemd unit hit its own memory limitRaise that limit or reduce what the app loads
swapon --show prints nothingNo swap is configuredCreate a swap file (steps below)
Swap is full and si/so in vmstat stay above zeroThe working set is larger than RAMTune the biggest process; if it persists, upgrade RAM
Kills continue after tuning and swapThe plan is too small for the workloadMove to more RAM
WordPress shows "Allowed memory size exhausted"PHP's own memory_limit, not the server running outSee 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 2G reserves 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 600 lets only root read it. Swap can hold copies of whatever was in memory, including passwords and keys.
  • mkswap writes the swap signature. swapon starts 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
  • ondemand starts workers only when requests arrive and ends idle ones after pm.process_idle_timeout (default 10s). It keeps idle memory low on a quiet site. dynamic keeps a set of spare workers ready.
  • pm.max_requests recycles 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 servicename and add [Service] with MemoryMax=1G to 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 5 shows si and so above zero during normal traffic, not only during a backup or an update;
  • available in free -h is close to zero at normal load;
  • cat /proc/pressure/memory shows a some line with avg60 above 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=0 to switch swap off. It lowers the kernel's use of swap and does not disable it. Remove swap with swapoff and the fstab line.
  • Making the swap file with cp or truncate. 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_children to fix 502 errors. More workers use more memory. Count what fits.
  • Setting OOMScoreAdjust=-1000 on a big service. The kernel then ends other services, or the server stops responding.
  • Changing vm.overcommit_memory or vm.panic_on_oom without testing. The kernel documentation says panic_on_oom=1 makes the kernel panic on an out-of-memory condition, and mode 2 of overcommit_memory applies 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 )

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

How do I check if the OOM killer killed my process?

Run sudo dmesg -T | grep -i -E 'out of memory|killed process', or sudo journalctl -k -g 'out of memory|killed process' if the server has restarted since. A match such as Out of memory: Killed process 2154 (mysqld) names the victim and its memory use. No match means something else stopped the process.

How does Linux decide which process the OOM killer ends?

Each process gets a score from 0 to 1000 in /proc/PID/oom_score, based mainly on how much memory it holds. The oom_score_adj value, from -1000 to +1000, is added to it. The highest score is killed first, so a large database can be chosen over small helper programs.

How big should a swap file be on a 2 GB VPS?

2 GB is a practical size for a 1 to 2 GB plan. It gives idle memory somewhere to go during a spike. Do not make it much larger to avoid upgrading: swap is disk-speed memory, and a server that fills it is still undersized.

Is vm.swappiness=0 the same as turning swap off?

No. The kernel documentation says that at 0, swapping starts only when free and file-backed pages fall below the zone's high watermark, so swap can still be used in an emergency. To disable swap, run swapoff on it and remove its line from /etc/fstab.

Why was MySQL killed and not PHP-FPM?

The kernel scores each process by memory use. MySQL holds the InnoDB buffer pool as one large allocation, so it can have the single highest score, while each PHP worker is small. Many PHP workers together can still cause the shortage. Check both in the log and size both.

Can I disable the OOM killer on a VPS?

You can change how it behaves, but turning it off is risky. If memory is exhausted and nothing is killed, the server can freeze or panic. The kernel offers panic_on_oom and overcommit_memory settings for this. Add swap, size your services, and lower oom_score_adj for one key service instead.

When should I upgrade RAM instead of adding swap?

Upgrade when kills or swapping return after you have set a swap file and sized PHP-FPM and MySQL, when vmstat shows si and so above zero at normal load, or when available memory in free -h is near zero. Swap covers a spike. It cannot cover a steady load that is larger than RAM.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.