VPS Swap Space: How Much You Need and How to Tune Swappiness

Swap is disk space the kernel uses when physical RAM is full — an emergency reservoir that prevents the OOM killer from terminating your processes. Used well, swap saves a MySQL server from being killed during a memory spike. Used badly, it turns an NVMe SSD into slow pretend-RAM and tanks performance. This guide explains how much swap a VPS actually needs and how to tune swappiness and cache pressure for your specific workload.

The right swap size depends on your RAM, workload, and disk type — and the same logic applies when you choose the VPS itself. If you are planning a memory-hungry deployment, compare VPS plans side by side in our comparison table to match RAM and storage to the job before you start tuning.

When Swap Helps (and When It Hurts)

Swap helps when a process briefly exceeds available RAM: the kernel pages out cold data, the process survives, and the spike passes. It hurts when the system relies on swap continuously, because every access to a swapped page costs disk latency — thousands of times slower than RAM. The goal of tuning is to keep swap as an insurance policy, never as part of the working set.

How Much Swap Do You Need?

For modern VPS workloads the old “2x RAM” rule is outdated. On SSD-backed VPS instances a useful baseline is:

VPS RAMRecommended swap (SSD/NVMe)
2 GB or less1–2 GB
4 GB2–4 GB
8 GB2–4 GB
16 GB or more0–4 GB (OOM insurance only)

For database servers, prefer extra RAM over extra swap. For bursty web workloads, keep 1–2 GB of swap to absorb traffic spikes without killing PHP-FPM workers.

Swapfile or Swap Partition?

A swapfile is easier to create, resize, and remove, and modern kernels handle it just as efficiently as a partition. A dedicated swap partition was the traditional choice on spinning disks. On a VPS, use a swapfile — you avoid repartitioning the virtual disk entirely, and resizing later is a two-minute job.

Creating a Swapfile

Create and enable a 2 GB swapfile on Ubuntu or Debian:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make it permanent in /etc/fstab:

/swapfile none swap sw 0 0

Verify with:

swapon --show
free -h

Tuning Swappiness

swappiness (0–100) controls how eagerly the kernel moves pages to swap. The default of 60 is too aggressive for most servers. Typical values:

vm.swappinessEffect
0–10Swap only to avoid OOM; keeps processes in RAM
10–30Good default for web and database servers
60Kernel default; too eager on most VPSes
100Aggressive swapping; not recommended on SSD

Set it for the running system and persist it across reboots:

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl --system

Tuning vm.vfs_cache_pressure

The kernel also caches directory and inode metadata. vfs_cache_pressure (default 100) controls how quickly that cache is reclaimed: values above 100 reclaim it more aggressively, values below 100 keep it longer. For file-heavy workloads on a server with spare RAM, 50 is a reasonable starting point:

echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.d/99-swap.conf
sudo sysctl --system

SSD Wear and Other Caveats

Every swap write wears the flash cells on an SSD. With swappiness at 10 and swap sized as insurance, the write amplification is negligible — the real risk is a misconfigured server that swaps constantly. Detect that with:

vmstat 2   # watch the si and so columns

If si/so are consistently non-zero, you need more RAM, not more swap. Also avoid placing swap on the same disk as a busy database when possible, and do not combine zram and a swapfile without understanding how they interact — running both can double the memory pressure you are trying to relieve.

Conclusion

Swap is cheap insurance: size it modestly, keep swappiness low, and treat constant swapping as a signal to buy more RAM. A 1–2 GB swapfile with vm.swappiness=10 handles traffic spikes on most VPS workloads without turning your SSD into a bottleneck. If you are sizing a new server, check the full specs and pricing in our comparison table and favor instances with enough RAM to make swap a fallback rather than a necessity.

Leave a Reply