Swap on a VPS is the safety net that keeps a small server alive when memory runs out, and the argument that slows it down. Both things are true, and the confusion comes from treating swap as either a fix or a failure. Swap is neither. It is disk space the kernel uses as overflow memory: slow, but better than killing a process. On a 1 or 2 GB VPS, a small swap file turns an out-of-memory crash into a slow afternoon, which is a trade worth making if you set it up right.
This guide explains when swap helps, how much to allocate for a small VPS, and how to add a swap file safely. It also explains when swap hurts, because a database that swaps constantly is slower than a database that crashes cleanly and restarts, and knowing the difference is the whole skill.
Swap on a VPS: When It Helps and When It Hurts
Swap helps in one situation: a short memory spike. A backup job, a plugin update, or a traffic burst pushes memory use over the limit for a few minutes, and swap absorbs the overflow. The server slows down, but it stays up, and the spike passes. Without swap, the kernel’s out-of-memory killer picks a process and ends it, which is often the database, and the site goes down until someone restarts it.
Swap hurts when it becomes permanent. A database that lives in swap answers queries from disk instead of RAM, and disk is orders of magnitude slower than memory. A server that swaps steadily is not saved by swap, it is strangled by it. The symptom is a load average that stays high while the CPU looks idle, and a site that feels broken rather than slow. If vmstat shows steady swap activity, the fix is less memory use, not more swap.
The rule is simple: swap is for spikes, not for capacity. If your server needs swap every day, it needs more RAM or fewer services, not a bigger swap file. If it needs swap twice a month, a swap file is the cheapest insurance you will ever buy.
One more case worth knowing: some apps refuse to start without swap, and some installers check for it. A small swap file satisfies those checks even if the server never uses it. The cost is a few gigabytes of disk, which is cheap on any modern VPS.
How Much Swap to Allocate
The old rule of twice the RAM comes from a time when servers had 512 MB of memory and disks were slow. It does not fit a modern VPS. A 2 GB server with 4 GB of swap spends its time swapping instead of working, and the disk fills up with pages the server will never read back. The modern guidance is smaller and depends on the RAM you have:
- 1 GB RAM: 1 GB of swap. The server is tight, and a full gigabyte of overflow covers a real spike without drowning the disk.
- 2 GB RAM: 2 GB of swap. Enough for a backup job or a traffic burst, small enough that the server cannot live in swap for long.
- 4 GB RAM: 2 GB of swap. At this size the server rarely needs swap at all, and 2 GB is enough for the rare spike.
- 8 GB RAM or more: 2 GB of swap, or none if the workload is steady. A server this size should not be swapping in normal use.
These numbers assume a typical VPS workload: a web server, a database, and a few small services. A server that runs memory-hungry work like video encoding or large builds wants a different answer, and the honest fix there is more RAM, not more swap.
Hibernation is the one exception to the small-swap rule, and it does not apply to a VPS. A laptop that hibernates writes all of RAM to swap, so it needs swap at least as large as RAM. A VPS never hibernates, so ignore that rule entirely.
Add a Swap File in Five Minutes
Most VPS providers ship no swap at all, so you add it yourself with a swap file. A file is better than a partition on a VPS, because you can resize or remove it without repartitioning the disk. The commands below create a 2 GB swap file on a 2 GB VPS:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
The chmod 600 step matters: a swap file that anyone can read is a security problem, because swapped pages can contain passwords and session data. mkswap formats the file, and swapon activates it. The free -h output should now show 2 GB of swap.
Make the swap file permanent, or it vanishes on the next reboot. Add one line to /etc/fstab:
/swapfile none swap sw 0 0
Test the fstab entry before you trust it. Run sudo swapoff /swapfile && sudo swapon -a && free -h. If swap comes back, the entry is correct. If it does not, check the line for a typo before you reboot, because a bad fstab entry can stop the server from booting.
If fallocate fails on your filesystem, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
It is slower, but it works on every filesystem. Some providers also disable swap at the host level, in which case swapon fails with an error about the operation not being permitted. If that happens, ask the provider whether swap is allowed, or move the workload to a server where it is. A related guide to adding swap on a VPS covers the same steps with a different setup.
Swappiness: How Eagerly the Kernel Swaps
Swappiness is a number from 0 to 100 that tells the kernel how eagerly to move memory to swap. The default on Ubuntu is 60, which is tuned for a desktop, not a server. On a VPS, 60 means the kernel starts swapping while RAM is still available, which slows the server for no reason. Lower it:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
A swappiness of 10 tells the kernel to swap only when memory is nearly full, which is what you want on a server. The database stays in RAM during normal use, and swap only catches the spike. A swappiness of 0 is too strict: it tells the kernel to avoid swap almost entirely, which defeats the purpose of having it. A swappiness of 1 is the same idea with a slightly softer edge. For a database server, 10 is the usual pick. For a server that runs mostly idle services, 10 is still fine.
Check the current value any time with cat /proc/sys/vm/swappiness. The setting in sysctl.conf survives a reboot; the sysctl -p command applies it now. If you change it and the server still swaps under normal load, the problem is memory use, not the setting.
Watch Swap Like Any Other Resource
Add swap to the list of things you check. free -h shows how much swap is used right now, and vmstat 2 5 shows whether it is being used at all: the si and so columns should read zero most of the time. A swap file that sits empty for months is doing its job. A swap file that fills up every day is a warning that the server needs more RAM or fewer services.
Memory tuning and swap tuning go together. A database with a buffer pool that is too big and a PHP pool with too many workers will swap no matter how you set swappiness, because the memory math does not add up. This guide to tuning MariaDB 11 on a low-memory VPS and this guide to choosing VPS specs for RAM, CPU, and storage cover the memory side, and this MySQL 8.4 tuning guide for a 2 GB VPS shows the same budget from the MySQL side.
The honest summary is short. Enable swap on a small VPS, keep it small, set swappiness to 10, and watch it. If it stays empty, you bought cheap insurance. If it fills up, you found the next thing to fix, and the fix is memory, not more swap. A server with the right amount of swap fails gracefully instead of crashing, and on a 2 GB VPS that difference is the whole point.
…[truncated 3010 chars]