Skip to content

Swap, vm.swappiness, and what the knob actually does

Swap is not a bigger RAM. Here is when swap helps a Linux production host, when it hurts, and what vm.swappiness really controls under the hood.

A database host on a 64 GB instance sees query latency climb during nightly batch jobs. Memory looks healthy — no OOM events, plenty of free RAM by free -m. But vmstat 1 shows si and so ticking up: the kernel is paging out anonymous memory even though RAM is not full. The fix is not “remove swap” and not “add more swap”. It is understanding what vm.swappiness does.

What’s happening

vm.swappiness is widely misunderstood as “how much swap is allowed”. It is not. It is a weighting between two kinds of memory the kernel can reclaim: anonymous pages (which must go to swap if pushed out) and file-backed pages (page cache, which can simply be dropped). A value of 60 — the historical default — means the kernel is fairly willing to swap out idle anonymous memory to keep file cache. A value of 1 does not disable swap; it tells the kernel to prefer dropping page cache and only swap anonymous memory when the alternative is failing the allocation.

This matters most on database hosts. PostgreSQL and MySQL ship with their own buffer pool and would rather the kernel hold on to their anonymous memory than evict it for someone else’s file cache. Hence vm.swappiness=1 on a DB host, and not zero — zero changes __GFP_IO semantics you usually do not want.

vm.vfs_cache_pressure is the sibling knob, controlling how aggressively the kernel reclaims slab caches. The default of 100 is fine for most workloads.

How to verify

Confirm what is actually configured and where swap lives:

swapon --show
cat /proc/swaps
sysctl vm.swappiness vm.vfs_cache_pressure
free -m

Watch swap traffic while the workload runs. si is swap-in, so is swap-out:

vmstat 1 20

A few so events at startup are normal. Sustained non-zero si and so mean continuous paging in both directions — thrash, surfacing as latency to anything touching disk. Cross-reference with per-process swap usage:

for pid in $(pgrep -f mysqld); do
  awk '/VmSwap/ {print pid, $0}' pid=$pid /proc/$pid/status
done

Check /proc/meminfo for SwapCached, Dirty, and Writeback — large SwapCached with low active traffic is fine; large Dirty plus rising so is not.

The fix

For a database or cache host, persist a low swappiness:

echo 'vm.swappiness=1' > /etc/sysctl.d/30-swap.conf
sysctl --system

For general-purpose application servers, vm.swappiness=10 is the common middle ground — swap stays available for genuine pressure, but the kernel will not preemptively page out idle code paths during quiet hours.

Right-size the swap device itself. On a cloud instance with predictable memory ceilings, a 2–4 GB swap file is a reasonable margin against transient spikes; 32 GB of swap on the same host is a slower way to fail. If swap is actively read from during normal operation, the host is undersized — resize, or move the workload.

Tuning the VM subsystem on hosts carrying production load is the kind of thing Cloud Compute builds in as a baseline — we can take a look.