Create a swap file on a Linux VPS

On a small VPS, a couple of GB of swap is cheap insurance against OOM kills. On a large one, swap is often more harmful than helpful. Here's when to use it, how to set it up, and the one sysctl that decides whether swap is friend or foe.

Disclosure: contains an affiliate link to Inferno VPS.

Do you actually want swap?

ScenarioSwap?
1 GB or 2 GB VPS running services that occasionally spikeYes — 1-2 GB swap file.
4-8 GB VPS, mixed servicesOptional — 1 GB cushion. Lower swappiness.
16 GB+ VPS for AI inference (Ollama)No — swapping models thrashes the disk and kills throughput.
Database serverNo — you'd rather Postgres OOM than start swapping query data.
VPS without local SSD (network block storage only)No — swap to network storage is brutally slow.

Create a swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
# Should now show 2 GB swap

chmod 600 matters — a world-readable swap file leaks process memory.

Persist across reboots

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify by rebooting (sudo reboot) and running free -h — swap should still be present.

The swappiness sysctl (the important part)

vm.swappiness controls how aggressively the kernel moves idle anonymous memory to swap. Default is 60 on most distros. For server workloads that's too high — the kernel swaps out idle process memory you'll need again, costing you the disk hit later.

sudo sysctl vm.swappiness=10              # apply now
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
# persist across reboots

10 is a common server value: only swap when memory is genuinely tight. For database hosts, drop further to 1 or even 0 (effectively "never swap unless OOM is imminent").

vm.vfs_cache_pressure

The companion setting. Lower values keep filesystem inode/dentry cache in RAM longer, faster directory traversals. Default 100 is fine for most servers; reduce to 50 on filesystem-heavy workloads.

Verifying swap actually helps

# Watch real-time
vmstat 5
# si (swap-in) and so (swap-out) should be low/zero most of the time
# Persistent non-zero values mean you're in trouble — add RAM or remove workload

Disabling swap on a busy box

If swap is harming you (constant swap-in/out causing I/O saturation):

sudo swapoff /swapfile
sudo sed -i '/swapfile/d' /etc/fstab
sudo rm /swapfile

Watch RAM use afterwards. If you're now bumping into OOM, your real problem is undersizing — upgrade the plan.

zram as a swap alternative

zram compresses RAM and uses it as a virtual swap device. Fast (no disk), useful on RAM-tight VPS:

sudo apt install -y zram-tools
# /etc/default/zramswap: PERCENT=50
sudo systemctl enable --now zramswap.service
zramctl

zram is meaningfully better than disk swap on small VPS plans. Combine with a small disk swap for overflow.

Common swap mistakes

"Server is swapping constantly and slow"

Your workload exceeds RAM. Swap is patching over an undersizing problem. Either remove a workload, lower memory limits on containers, or upgrade the plan.

"Swap exists but never used"

swappiness is at 0 or too low for your workload. Either accept it (you may genuinely never need swap) or raise to 10.

"swap on /swapfile but free -h shows 0 swap"

You forgot swapon /swapfile, or the file's mode is wrong. swapon --show reveals the actual state.

"Postgres unhappy with swap enabled"

Databases prefer "OOM here and now" to "creeping latency from page-ins." Drop swappiness to 1 on database hosts.

Need a bigger VPS instead of fighting swap?

Inferno's 4 GB plan ($7.99/mo) gives you the headroom for a typical n8n + Postgres setup without needing swap. Stepping up is often cheaper than the wall-clock cost of swap-related slowdowns.

View Inferno plans →

What to do next


Author: Hanna Wagner — Frankfurt. Last tested: 2026-05-22 on Ubuntu 22.04, kernel 5.15.