Swap on a VPS: when you need it and how to configure it without slowing your disk
How to create a swap file on Ubuntu, what size to choose based on RAM, and why you should lower vm.swappiness on an SSD-backed VPS.
UptimeMag editorial team · 17 October 2026 · 3 min read

On a VPS with limited RAM, swap prevents the OOM killer from terminating processes when memory runs out. On a VPS with SSD storage, however, overly aggressive swap degrades response times instead of protecting them: the disk, no matter how fast, remains orders of magnitude slower than RAM. The guide below covers sizing, creation, the swappiness parameter and verification.
How much swap you need, based on RAM
The most commonly cited recommendations for server systems (not desktop, so without hibernation assumptions) are these:
| VPS RAM | Recommended swap |
|---|---|
| Less than 2 GB | 2× RAM |
| 2 to 8 GB | Equal to RAM |
| Over 8 GB | At least 4 GB |
On a server, unlike a desktop, you don't need to size swap for hibernation: the practical recommendation for a server environment is not to exceed a few GB, because beyond a certain threshold the system is only slowing down rather than gaining real headroom. If your VPS constantly exhausts even 4–8 GB of swap, the problem isn't solved by enlarging the file: you need to increase the RAM on your plan.
Creating and activating the swap file
On Ubuntu, as root or with sudo:
# create a 2 GB swap file (adjust the size as needed)
sudo fallocate -l 2G /swapfile
# permissions: only root should be able to read it
sudo chmod 600 /swapfile
# format as swap area
sudo mkswap /swapfile
# activate the swap
sudo swapon /swapfile
If the filesystem doesn't support fallocate (happens with some btrfs setups), use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
count=2048 with 1 M blocks creates a 2 GB file; for 4 GB use count=4096.
To make the swap persistent across reboots, add a line to /etc/fstab:
/swapfile none swap sw 0 0
vm.swappiness: why lower it on SSD
The vm.swappiness parameter tells the kernel how much to prefer swap over freeing filesystem cache. The default value on nearly all distributions is 60. Before kernel 5.8 the allowed range was 0–100; from kernel 5.8 onwards the range was extended to 0–200, with 100 as the equilibrium point where the cost of I/O on swap and filesystem are considered equivalent. On older kernels a value above 100 is rejected with an error: on a VPS with an older image (e.g. Ubuntu 20.04, kernel 5.4) you should verify this before setting it.
On a server with SSD, where the goal is to keep processes active in RAM as much as possible, it makes sense to lower the value to 10–30, rather than leaving the default designed for generic desktop workloads.
Commands:
# check the current value
cat /proc/sys/vm/swappiness
# set a temporary value (until reboot)
sudo sysctl -w 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
Verify that swap is active and how much is being used
# show active swap devices and how much is occupied
swapon --show
# overview of RAM and swap
free -h
# raw contents, same info as swapon
cat /proc/swaps
If swapon --show returns nothing, swap is not active: check that the file exists, has 600 permissions and was actually activated with swapon. If the "Used" field in free -h grows constantly even with free RAM available, the most likely problem is not the swap size but a swappiness value that's too high for your server's workload.
Written with the help of artificial intelligence and checked by the editors (EU AI Act, art. 50).