Introduction
The morning surprise every VPS owner dreads: you log in and half your services are dead. Docker containers exited, the database stopped, nginx is not running. You check the logs and find the same signature everywhere: the kernel’s OOM killer decided your processes were the sacrifice needed to keep the system alive. This is not a bug and not a hack; it is Linux doing exactly what it was designed to do when memory runs out. The good news: it leaves clear evidence, and the fix (a swap file plus a few settings) takes ten minutes. This guide walks through reading that evidence and setting up swap properly.
What the OOM Killer Actually Does
When the kernel is about to run out of memory and cannot reclaim enough through normal means (dropping caches, shrinking file-backed pages), it invokes the Out-Of-Memory killer. The killer’s job is simple: pick a process, kill it, and free its memory immediately, so the rest of the system survives.
How does it pick the victim? Each process gets an oom_score based mainly on how much memory it uses: the bigger the consumer, the higher the score, the more likely it gets killed. That is why the database is usually the first to go: it is typically the largest single process on the box. The score can be adjusted through oom_score_adj, which is how critical processes get protected, but tuning that is treating the symptom. The real question is why memory pressure got that high in the first place.

Read the Evidence First
Before changing anything, confirm the OOM killer is actually what happened:
sudo dmesg -T | grep -i "killed process" or, on systems where the ring buffer has already rotated:
sudo journalctl -k --since "yesterday" | grep -i "oom" The output names the killed process and shows the memory state at that moment. Then check where memory stands now:
free -h Look at two lines: available memory (not the misleading “free” column) and swap. A VPS with zero swap and an application that spikes (a backup job, a big SQL query, a traffic burst) has no shock absorber at all. Any transient spike goes straight to the OOM killer. That is the most common scenario on small plans, and it is fixable.
Add Swap on a VPS
Swap gives the kernel a place to park idle pages, which changes the memory picture completely: transient spikes get absorbed instead of answered with a kill. Create a 2GB swap file:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileMake it survive reboots by adding to /etc/fstab:
/swapfile none swap sw 0 0 A note for container-based VPS: this works on KVM virtualization, where each VM has its own kernel. OpenVZ containers share the host kernel and cannot create their own swap. If you are on OpenVZ and hitting OOM kills, your options are reducing memory usage or upgrading the plan. We compared the two virtualization types in detail in the “KVM vs OpenVZ: Which VPS Virtualization Should You Choose in 2026?” article.
Tune Swappiness, Not Just Add Swap
The kernel parameter vm.swappiness controls how eagerly pages are moved to swap, from 0 (almost never) to 100 (aggressively). On a VPS with a real workload, the common recommendation is a low value:
sudo sysctl vm.swappiness=10 Persist it in /etc/sysctl.conf. The reasoning: swap on SSD-backed VPS is fast enough for idle pages, but you do not want active pages constantly bouncing. A low swappiness means swap absorbs spikes and idle background processes, while the hot working set stays in RAM.
Swap Is a Buffer, Not More RAM
Honest framing matters here: swap does not add memory capacity. It buys time for spikes and keeps idle pages from wasting RAM; that is all. If your workload genuinely outgrows the plan, no swappiness value will save you; the OOM kills will just move further apart. At that point the right move is an upgrade: my baseline reference is VMRack’s Los Angeles VPS line, where the entry plan starts at $6.5/month and the same account scales up through the optimized and premium China-route tiers without a migration. And watch for the pathological case: memory usage that climbs steadily and never plateaus is a leak, and swap only delays the crash. Fixes belong in the application, not the kernel.
Conclusion
The OOM killer is the kernel trading one process for the whole system’s survival: it picks the biggest memory consumer, which is usually your database. When services die overnight, read the evidence first: dmesg or journalctl for the kill record, free -h for the memory state. Zero swap plus a memory spike is the classic small-VPS recipe, and a 2GB swap file with swappiness set to 10 fixes most of those cases. Remember what swap is not: it is not extra RAM, and it does not patch leaks. Add the buffer, watch the trend, upgrade when the workload truly outgrows the plan.
Thank you for reading to the end!