Swap is disk space the kernel uses as overflow when RAM is full. On a server it’s a safety net. It absorbs memory spikes so the OOM killer doesn’t kill your database. It isn’t a substitute for RAM, because disk is thousands of times slower. Here’s how to check it, add it, tune it and interpret it.
Check swap
free -h
swapon --show
total used free shared buff/cache available
Mem: 3.8Gi 2.1Gi 220Mi 60Mi 1.5Gi 1.4Gi
Swap: 2.0Gi 310Mi 1.7Gi
NAME TYPE SIZE USED PRIO
/swapfile file 2G 310.2M -2
Is swap usage a problem?
Used swap alone isn’t a problem. The kernel moves rarely used memory pages (idle daemons, startup code) to swap to free RAM for cache. What matters is activity: pages moving in and out continuously.
vmstat 1 5
Watch si (swap in) and so (swap out). Zero, or occasional small numbers, is fine. Continuous non-zero values, especially si, mean the working set doesn’t fit in RAM. The server is thrashing, and everything slows down. See vmstat.
Which processes are in swap?
for f in /proc/[0-9]*/status; do
awk '/^Name/{n=$2} /^VmSwap/{if($2>0) print $2" kB", n}' "$f"
done 2>/dev/null | sort -rn | head
Or interactively, add the SWAP column in htop (F2 → Columns), or use smem -s swap.
Add a swap file
Many VPS images come without swap. Adding a swap file takes a minute:
sudo fallocate -l 2G /swapfile # or: sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
swapon --show
chmod 600matters, because swap can contain sensitive memory contents.- On Btrfs, swap files need special handling:
btrfs filesystem mkswapfile --size 2g /swapfile(btrfs-progs 6.1+), or a dedicated subvolume with CoW disabled. - If
fallocategives “swapon: … has holes” on some filesystems, usedd.
Resize the swap file
sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile # or recreate with dd
sudo mkswap /swapfile
sudo swapon /swapfile
swapoff needs enough free RAM to take back everything currently in swap.
How much swap?
There’s no single rule for servers. Practical guidance:
| RAM | Swap suggestion |
|---|---|
| ≤ 2 GB | 1–2 GB |
| 4–8 GB | 1–4 GB |
| 16 GB+ | 2–4 GB (a buffer, not capacity) |
| Hibernation | Not relevant for servers |
The goal is a cushion for spikes, plus room for idle pages, not running the workload from disk.
Swappiness: how eager to swap
cat /proc/sys/vm/swappiness # default 60
Lower values make the kernel prefer dropping file cache over swapping out application memory. For servers running databases and app processes, 10 is a common choice:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
Avoid 0. It can lead to OOM kills where a little swapping would have saved the day. See sysctl tuning.
Clear swap
After a memory spike has passed, swapped pages come back gradually as they’re used. To force it (for example before a benchmark):
free -h # make sure "available" RAM > used swap
sudo swapoff -a && sudo swapon -a
If there isn’t enough free RAM, swapoff can trigger the OOM killer, so check first.
Remove swap
sudo swapoff /swapfile
sudo sed -i '\#^/swapfile #d' /etc/fstab
sudo rm /swapfile
zram: compressed swap in RAM
zram creates a compressed swap device in memory. It’s much faster than disk swap and effectively stretches RAM by 2–3× for compressible data. It’s useful on small VPS plans. See zram swap.
When swapping means “upgrade”
If si/so is busy during normal operation and tuning (PHP-FPM workers, database buffers, app memory) doesn’t fix it, the server needs more RAM. VPS.DO plans scale from 2 GB to 40 GB of RAM. See how much RAM a VPS needs and OOM killer.
Quick reference
| Task | Command |
|---|---|
| Show swap | free -h / swapon --show |
| Swap activity | vmstat 1 (si/so) |
| Add 2 GB file | fallocate -l 2G /swapfile; chmod 600; mkswap; swapon |
| Persist | /swapfile none swap sw 0 0 in fstab |
| Swappiness | sysctl vm.swappiness=10 |
| Clear | swapoff -a && swapon -a |
Related: free command and fstab.