Swap on a VPS — when to use it, how much, and how to set it up
Swap on a VPS lives in a fuzzy zone of folk wisdom: "you don't need swap if you have enough RAM," "swap will thrash and kill performance," "but actually you should always have some for kernel reasons." The right answer depends on what you're running, and "always at least a little, mostly for safety margin" is the practical default. This article walks through the decision and the setup.
What swap actually does on Linux
The kernel uses swap for three purposes that often get conflated:
- Demand paging: moving cold pages (process memory that hasn't been touched in a while) out to disk so RAM is available for active work. Even at low memory pressure, the kernel may swap out cold pages — improves overall throughput by freeing RAM for filesystem cache.
- OOM safety margin: when free memory hits zero, the OOM killer fires and kills processes. Swap delays that, giving you time to notice and intervene before something important dies.
- Hibernation: writing RAM contents to disk for suspend-to-disk. Doesn't apply to VPSes.
The "swap makes things slow" trope conflates #1 and #2 with thrashing — the pathological state where the system spends more time swapping pages in and out than doing useful work. Thrashing happens when active working set exceeds RAM regardless of swap; swap doesn't cause it, RAM shortage does. Swap just changes the failure mode from instant OOM to slow thrash.
The rule of thumb
| RAM | Recommended swap | Why |
|---|---|---|
| 1–2 GB | 1 GB | Tight environments; swap gives meaningful headroom. |
| 4–8 GB | 2 GB | Enough to absorb a memory spike before OOM. |
| 16–32 GB | 2–4 GB | Symbolic; most apps that fit in 16+ GB will OOM-kill long before they touch swap. |
| 64+ GB | 2 GB or none | Diminishing returns; safety margin only. |
The PBX VPS line gets 1 GB swap configured at deploy because FreePBX®'s fwconsole --check warns when no swap is configured — even on hosts that don't strictly need it, the noise saves a support ticket.
Creating a swapfile (preferred over a partition on a VPS)
On a VPS you don't repartition; you allocate a file and tell the kernel to use it as swap. The process is identical across distros.
# Allocate 2 GB swap file
fallocate -l 2G /swapfile # fast on ext4/xfs; on older fs use dd
chmod 600 /swapfile # not world-readable -- it'll contain memory pages
mkswap /swapfile # initialize swap structures
swapon /swapfile # activate
# Persist across reboots
echo '/swapfile none swap sw 0 0' >> /etc/fstab
# Verify
swapon --show
free -h
If fallocate fails ("fallocate failed: Operation not supported"), the filesystem doesn't support it — fall back to dd if=/dev/zero of=/swapfile bs=1M count=2048. Slower but works everywhere.
Tuning: swappiness
The vm.swappiness sysctl (0–100) controls how aggressively the kernel uses swap for cold-page eviction:
- 60 (default) — balanced, fine for most workloads.
- 10 — bias toward keeping pages in RAM; swap only under pressure. Good for databases and caches where the working set should stay hot.
- 1 — swap is for OOM safety only; nothing gets swapped proactively.
- 100 — aggressive swap, sometimes recommended for desktop / interactive workloads to keep filesystem cache fat.
For a VPS running databases or PBX workloads, drop to 10:
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.conf
sysctl --system
Removing swap
If you decide you don't want it:
swapoff /swapfile
sed -i '/\/swapfile/d' /etc/fstab
rm /swapfile
The swapoff may take a while if there's anything actually in swap — the kernel has to page it all back to RAM first. Don't be alarmed.
Monitoring swap usage
Run free -h and look at "used" under Swap. A few hundred MB in active use on a healthy system is normal (the kernel evicted cold pages). Gigabytes in use with high si/so in vmstat 1 is thrashing — you're undersized on RAM, swap is the symptom not the cause.
# Per-process swap usage (Debian/Ubuntu, recent kernels)
for pid in $(pgrep -u nobody); do
swap=$(awk '/VmSwap/{print $2}' /proc/$pid/status 2>/dev/null)
echo "$pid: ${swap:-0} kB"
done | sort -t: -k2 -n | tail -10
Also Read
Powered by WHMCompleteSolution