How Eager Are You to Swap?
You might think Linux only uses swap when RAM is 100% full. That's rarely true. Linux aggressively uses free RAM as page cache — it caches recently read disk files in RAM to speed up future reads. This is why free -h often shows very little "free" memory on a healthy server: the kernel has filled it with useful cache.
The kernel faces a constant trade-off: when memory pressure builds, should it evict page cache (cached disk files) or anonymous memory (heap/stack data from running processes, which must go to swap)?
This trade-off is controlled by vm.swappiness, a value from 0 to 200 (kernel 5.8+; previously 0–100).
| Value | Behavior |
|---|---|
0 |
Strongly prefer evicting page cache over swapping anonymous memory. Only swap to avoid OOM. |
60 |
Default. Balanced approach. |
100 |
Treat page cache and anonymous memory equally when choosing what to evict. |
200 |
Aggressively swap anonymous memory to maximize page cache retention. |
Common tuning recommendations:
- Latency-sensitive databases (PostgreSQL, MySQL, Redis): Set to 10 or lower. These applications manage their own caches and are devastated by swap latency.
- General-purpose servers: Leave at 60.
- Desktop systems doing heavy file I/O: Consider 80–100.
Current swappiness is readable from /proc/sys/vm/swappiness, and can be changed at runtime (until reboot) via sysctl vm.swappiness=<value>.
Important: Setting swappiness to 0 does not disable swap. It makes swap a last resort, but the kernel will still use it to avoid an OOM condition. To fully disable swap, use sudo swapoff -a.