Linux Internals
Swappiness and Swap on a VPS: A Practical Linux Guide
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيFew Linux settings attract as much folklore as swap. Some guides say a server should never swap, others say you must always have swap, and vm.swappiness is often described as a dial for "how much the system uses swap". The reality is more nuanced and more useful. This guide explains what swap does on a VPS, what swappiness actually controls, how to add a swap file safely and how to judge whether swapping is a problem on your server.
What swap is for
Linux keeps two broad kinds of pages in RAM. File-backed pages are copies of data from disk, such as program binaries, libraries and the page cache. Anonymous pages hold data that exists only in memory, like a process's heap and stack.
When memory runs short, the kernel reclaims pages. File-backed pages that are unchanged can simply be dropped and read again later. Anonymous pages have nowhere to go unless there is swap. Without swap, the kernel can only reclaim file pages, so it ends up evicting useful cache and code while idle anonymous memory, perhaps from a daemon that ran once at boot, sits in RAM forever.
Swap gives the kernel the choice to move those cold anonymous pages out, leaving more RAM for active data and cache. It also gives you a buffer during short spikes before the OOM killer acts, as described in memory overcommit and the OOM killer.
What swappiness really means
vm.swappiness tells the kernel how to weigh reclaiming anonymous pages (swapping) against reclaiming file pages (dropping cache). On current kernels it accepts values from 0 to 200, and the default is 60.
cat /proc/sys/vm/swappiness- Lower values favour dropping cache and keep application memory in RAM longer.
- Higher values make the kernel more willing to swap out idle anonymous memory, which can make sense when swap is fast, such as zram.
- A value of 0 does not disable swap. It strongly discourages it, but the kernel can still swap under pressure.
Swappiness only matters when the kernel is already reclaiming memory. On a server with plenty of free RAM it changes nothing.
Adding a swap file
Many VPS images ship without swap. A swap file is the simplest option:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabOn filesystems where fallocate files are not valid swap, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. Btrfs needs extra steps, so check its documentation. Confirm with:
swapon --show
free -hHow big should it be?
There is no universal formula. For most VPS workloads, swap is a safety cushion rather than extra memory, so a size between half and equal to your RAM, capped at a few gigabytes, is a reasonable starting point. Hibernation, which needs swap at least as large as RAM, does not apply to servers.
Setting swappiness
Change it at runtime, then persist it:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system| Workload | Suggested starting value | Reason |
|---|---|---|
| General web server | 60 (default) or 30 | Default works well; lower if cache is less important |
| Database (MySQL, PostgreSQL) | 1-10 | Keep buffer pools in RAM; the DB manages its own cache |
| Desktop-style or many idle daemons | 60 | Let idle memory move out |
| zram as swap | 100-180 | Compressed swap in RAM is cheap to access |
Treat these as starting points and measure.
Is swapping actually hurting you?
Swap usage on its own is not a problem. A server with 300 MB in swap that never reads it back is healthy: those are cold pages. What hurts is continuous swapping in and out. Watch the si and so columns:
vmstat 1If si and so are mostly zero, relax. If they are steadily non-zero while response times climb, the working set does not fit in RAM. Pressure stall information gives a clearer picture:
cat /proc/pressure/memoryRising some and full averages mean tasks are waiting on memory. To see which processes hold swap:
for f in /proc/[0-9]*/status; do awk '/^Name|^VmSwap/{printf "%s ", $2} END{print ""}' "$f"; done | sort -k2 -n | tailzram as an alternative
zram creates a compressed block device in RAM and uses it as swap. It trades some CPU for avoiding disk I/O entirely, which suits small VPS plans. On Debian and Ubuntu, the zram-tools or systemd-zram-generator packages set it up. Combine it with a higher swappiness value, since swapping to zram is relatively cheap.
Checklist
- Add a modest swap file if your VPS has none.
- Keep the default swappiness unless you have a reason, such as a database server.
- Monitor
vmstatsi/soand/proc/pressure/memory, not just swap usage. - If you swap constantly under normal load, you need more RAM. Compare plans on the pricing page.
FAQ
Does swappiness=0 disable swap?
No. It tells the kernel to avoid swapping as long as possible, but swap can still be used under memory pressure. To disable swap entirely, run swapoff -a and remove the entry from /etc/fstab, which is rarely a good idea on a server.
Is swap bad for SSD or NVMe storage?
Occasional swapping causes negligible wear on modern SSDs. Constant heavy swapping is a performance problem first, and it signals that the server needs more memory.
Why is swap used when I still have free RAM?
The kernel may have moved idle pages out earlier during a memory spike, and it does not bring them back until they are needed. This is normal and does not by itself indicate a problem.