Swap is the emergency reservoir Linux uses when physical RAM runs out — and on a VPS it is either a lifesaver or the thing that makes your server feel ten times slower. The difference is not just how much swap you configure, but what kind: a swapfile on disk, a swap partition, or zram compressed RAM. This guide covers creating and enabling each, monitoring real swap pressure, and the swappiness defaults that keep the kernel from leaning on disk unnecessarily.
Swapfile, partition, or zram?
| Type | Speed | Best use case |
|---|---|---|
| Swapfile | Disk I/O speed | Default choice on VPSes; easy to create and resize |
| Swap partition | Disk I/O speed | Only if the provider image already includes one; hard to resize |
| zram | RAM speed (compressed) | Small VPS with tight disk I/O; bursty memory spikes |
A swapfile costs nothing until used and takes minutes to create on any filesystem that supports it (ext4, xfs, btrfs). A partition requires repartitioning the disk, which most VPS control panels make awkward. zram uses a slice of RAM compressed roughly 2–4x, so it never touches disk — at the cost of some CPU and usable RAM.
Creating a swapfile
- Check existing swap:
swapon --show(empty output = none) andfree -h. - Create the file:
sudo fallocate -l 2G /swapfile. Iffallocatefails on your filesystem, usesudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress. - Restrict permissions:
sudo chmod 600 /swapfile— world-readable swap can expose credentials that were swapped out. - Format it:
sudo mkswap /swapfile - Enable it:
sudo swapon /swapfile - Make it persistent: add
/swapfile none swap sw 0 0to/etc/fstab. - Verify:
sudo swapon --showandfree -h— the Swap line should show your new size.
How much swap is enough
The old “2x RAM” rule is wrong for VPSes. Swap on a small plan shares disk I/O with your real workload, so oversized swap turns a memory crisis into a disk crisis. Practical sizing: 1–2 GB on plans up to 4 GB RAM — enough to ride out spikes and keep the OOM killer idle — and 2–4 GB on larger plans for safety. If an application consistently needs more than that, buy more RAM instead: swap is a safety net, not a RAM upgrade.
zram: compressed swap in RAM
- Install:
sudo apt install -y zram-tools(Debian/Ubuntu). - Configure
/etc/default/zramswap: setALGO=zstdandSIZE=1024(MiB) — start at about half your free RAM. - Start it:
sudo systemctl enable --now zramswap - Verify:
swapon --showshould list/dev/zram0.
zram shines on small VPSes where a memory spike would otherwise push pages to a shared disk: the “swap” stays in RAM, so recovering from a spike is fast. It is a poor fit when you genuinely need more memory than the box has — for example a database larger than RAM — because then disk swap is the only option and the right fix is a bigger plan.
Monitoring swap pressure
free -h— how much swap is in use. Used swap is not automatically bad; the kernel parks cold pages there.vmstat 1— thesiandsocolumns show pages swapped in and out per second. Sustained non-zero values mean real pressure and disk-bound thrash.cat /proc/pressure/memory— PSI (pressure stall information): thesome avg10line shows the percentage of time processes stalled on memory. Above roughly 10% sustained means memory is the bottleneck.swapon --show— which devices are active and their priority.
Rule of thumb: occasional si/so activity during a spike is fine. If both columns are continuously non-zero while free shows RAM nearly full, you are out of memory and swap is papering over it — the fix is more RAM or a smaller footprint, not more swap.
Swappiness defaults that matter
The kernel’s vm.swappiness parameter (default 60) controls how eagerly the kernel swaps anonymous memory versus reclaiming page cache. On VPSes with disk-backed swap, a high value causes needless disk I/O; most server workloads run better at 10. Check yours with sysctl vm.swappiness. Because a value of 0 does not fully disable swapping (the kernel still swaps to avoid the OOM killer), 1–10 is the practical floor for latency-sensitive applications. Deep per-workload tuning of this parameter is its own topic — the defaults above already keep the kernel from treating your swapfile as a scratch disk.
Configure swap before you need it: an OOM kill during a traffic spike is a 2 a.m. emergency, while a 2 GB swapfile is a two-minute task now. And when you size your next plan, remember that swap depends on fast storage: see the full specs and pricing on storage type before you decide, and compare providers on our comparison table — a plan with NVMe-backed swap recovers from memory spikes far faster than one on shared, slower storage.


Leave a Reply
You must be logged in to post a comment.