Linux swap and OOM tuning for small VPS plans
Out of memory? Learn how swap works, how to add a swap file, tune swappiness, and understand the OOM killer so your small VPS stays stable under pressure.
On a small VPS, memory is the resource you run out of first โ and when you do, Linux either grinds to a halt or starts killing processes. Understanding swap and the OOM killer lets you keep a low-RAM server stable under pressure. Here's what's going on and how to tune it.
What swap actually does
Swap is disk space the kernel uses as overflow when RAM fills up. Inactive memory pages get moved to swap, freeing RAM for active work. Swap is much slower than RAM (it's disk), so it's a safety cushion, not a substitute for real memory โ but that cushion often prevents crashes.
Check current memory and swap:
free -h
Many VPS images ship with no swap at all, which means a memory spike has nowhere to go.
The OOM killer
When memory is truly exhausted and there's no swap left, the kernel invokes the Out-Of-Memory killer. It picks a process โ usually the biggest memory user โ and terminates it to save the system. That's often your database or web app dying without warning. You can see its work in the logs:
sudo dmesg | grep -i "killed process"
sudo journalctl -k | grep -i oom
If you find your app mysteriously restarting, the OOM killer is a prime suspect.
Add a swap file
Adding swap is the single most effective stability fix on a small VPS. Create a 2 GB swap file:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Verify it's active:
free -h
swapon --show
Make it permanent so it survives reboots by adding to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
A common sizing guideline: swap equal to your RAM for small plans (1โ2x), less as RAM grows.
Tune swappiness
Swappiness (0โ100) controls how eagerly the kernel swaps. The default of 60 is aggressive for a server โ it swaps out memory that could stay in RAM, hurting performance. Lower it so swap is used mainly as a last resort:
sudo sysctl vm.swappiness=10
Make it permanent:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
A value of 10 means "keep things in RAM until you really must swap" โ the right posture for most servers.
NVMe makes swap far less painful
Swap's downside is disk speed, and this is where storage type matters enormously. On slow SATA disks, hitting swap can make a server feel frozen. On NVMe โ which every Nxeon plan uses โ swap is dramatically faster, so the safety cushion actually stays usable rather than crippling the box. It's still no replacement for RAM, but the penalty is far smaller.
Reduce memory pressure at the source
Swap buys headroom, but also trim what's eating memory:
- Right-size service limits. Cap PHP-FPM workers, database buffer pools and app worker counts to fit your RAM instead of letting them balloon.
- Enable a page cache so your app does less work per request.
- Find the hogs:
ps aux --sort=-%mem | headshows the top memory consumers to investigate.
Protect a critical process from the OOM killer
If one process must never be killed, you can bias the OOM killer away from it via its oom_score_adj (lower is safer). For a systemd service, set in its unit:
[Service]
OOMScoreAdjust=-500
Use this sparingly โ it just shifts which process gets sacrificed.
When tuning isn't enough
Swap and tuning stabilize a server that occasionally spikes. But if free -h shows memory *constantly* full and swap heavily used, that's not a tuning problem โ you've genuinely outgrown the plan. On Nxeon you can resize the VPS to add RAM without rebuilding, which is the real fix once you're consistently over budget.
The quick recipe
For any small VPS: add a 2 GB swap file, set swappiness to 10, cap your services' memory use, and keep an eye on free -h. That combination keeps a modest server stable through the memory spikes that would otherwise take it down.