The Most Misunderstood Knob
Imagine your desk has limited space (RAM). You have two types of things on it: open books you're actively reading (file cache) and your personal notebooks with your own work (application memory). When the desk gets full, you have to move something to a shelf (swap on disk). vm.swappiness controls which type of thing you move to the shelf first.
vm.swappiness is an integer from 0 to 200 (0 to 100 on kernels before 5.8) that controls the kernel's preference between reclaiming two types of memory pages:
- Page Cache (File-backed pages): Copies of files read from disk. These can be dropped and re-read from disk later — no data is lost.
- Anonymous Memory (Anon pages): Program heap and stack data. This has no backing file, so it must be written to swap before it can be evicted.
The myth: "Setting vm.swappiness=0 disables swap."
The reality: It makes the kernel extremely reluctant to swap anonymous pages, but it will still use swap if there is genuinely no other option. Swap is never fully disabled by swappiness alone — you disable it with swapoff -a.
How the value affects behavior:
| Value | Behavior |
|---|---|
| 0–10 | Kernel strongly prefers dropping file cache over swapping anonymous pages. Applications stay in RAM longer. File I/O may slow down. |
| 60 (default) | Balanced. The kernel weighs both options roughly equally. |
| 100–200 | Kernel aggressively swaps anonymous pages to keep file cache warm. Good for I/O-heavy workloads; bad for interactive applications. |
Practical recommendations: databases like PostgreSQL or MySQL manage their own buffer pools and perform catastrophically when their working set gets swapped out, so they're typically tuned down to vm.swappiness=1. An entirely in-memory store like Redis gets the same treatment — any swapping kills its latency. A general-purpose application server usually lands around 10, while a desktop workstation is fine left at the default of 60.
The senior-level nuance here is that vm.swappiness interacts with vm.vfs_cache_pressure, which controls how aggressively the kernel reclaims inode and dentry caches. On a database server, you might set both: vm.swappiness=1 and vm.vfs_cache_pressure=50 to further protect the kernel's VFS cache. Tuning one parameter in isolation often misses the full picture.