The single most common myth about Linux memory is that vm.swappiness=0 turns swap off. It does not. Swappiness is not a threshold, a percentage of RAM, or an on/off switch — it is the kernel’s relative preference for reclaiming anonymous pages (your programs’ heap and stack) versus dropping page cache (file data the kernel is holding in RAM). A low value tells the kernel “when you need to free memory, drop cache before you swap.” A value of 0 discourages swapping as strongly as the dial allows, but the kernel will still swap anonymous pages rather than invoke the OOM killer when it is genuinely out of memory. If you want swap truly off, you run swapoff, not swappiness=0. This guide covers what the dial really does, how to add a swap file the safe way, how to set the value so it survives a reboot, and how to read actual memory pressure instead of guessing.
What you’ll accomplish
- Understand
vm.swappinessas a reclaim preference on a 0–200 scale, not a percentage or a threshold. - Add a correctly sized, correctly permissioned swap file and register it in
/etc/fstab. - Set
vm.swappinessandvm.vfs_cache_pressurelive and persist them across reboots. - Read real memory pressure with
vmstat,free -h, and the PSI file/proc/pressure/memory. - Choose a sane swap policy for a database host versus a general-purpose server.
Prerequisites
- A Linux server with
rootorsudo. - Kernel 4.20 or newer if you want Pressure Stall Information (
/proc/pressure/memory); it is enabled by default on most current distributions. - On cgroup v2 systems, per-service PSI is also available under
/sys/fs/cgroup/<slice>/memory.pressure. - Extra care on database hosts: changing swap behaviour under a live workload can move latency in ways you must be ready to observe and revert.
How it works
When the kernel runs low on free RAM it reclaims pages to make room. It has two broad sources. Page cache is file-backed: clean cached pages can simply be dropped and re-read from disk later, which is cheap. Anonymous pages — process heap, stack, malloc’d memory — have no file behind them, so to reclaim them the kernel must first write them to swap, which is comparatively expensive.
vm.swappiness is the dial that biases this choice. On modern kernels (5.8+) its range is 0 to 200; older kernels cap at 100. The default is usually 60. A higher value makes the kernel more willing to swap anonymous pages so it can keep more page cache; a lower value makes it prefer reclaiming cache and leave anonymous memory resident. Crucially, 0 does not mean “never swap” — it means “avoid swapping until doing so is the only way to avoid an out-of-memory situation.” The kernel still swaps under real pressure.
A companion knob, vm.vfs_cache_pressure, controls how aggressively the kernel reclaims the memory used for directory and inode caches (dentries and inodes). The default is 100. Lowering it makes the kernel keep filesystem metadata cached longer; raising it reclaims that metadata sooner. It is a separate lever from swappiness and is worth tuning only on workloads that walk large directory trees.
The number that tells you whether swapping is actually hurting you is not swap usage — a few used megabytes of long-idle pages is harmless. What matters is active swap traffic and stall time. vmstat exposes the si/so columns (swap-in/swap-out per second). PSI, in /proc/pressure/memory, reports the share of time tasks stalled waiting on memory. Non-zero si/so under load plus a rising PSI some avg10 is real pressure; idle swap with quiet columns is not.
Step-by-step
-
Check what you have now. Read the current swappiness, memory layout, and any active swap devices.
cat /proc/sys/vm/swappiness free -h swapon --showExpected: swappiness prints a number (often
60);free -hshows aSwap:row;swapon --showlists devices, or prints nothing if no swap is configured. -
Create a swap file. Prefer
fallocatefor speed; fall back toddif your filesystem does not support it well (olderext4on some kernels, or when you hitswapon: swapfile has holes). This example makes a 4 GiB file.sudo fallocate -l 4G /swapfileIf
swaponlater complains about holes, recreate it withddinstead:sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progressExpected: a
/swapfileof the requested size (ls -lh /swapfileshows4.0G). -
Lock down permissions, then format it as swap. The file must be readable only by root, or
swaponwill refuse it.sudo chmod 0600 /swapfile sudo mkswap /swapfileExpected:
mkswapprintsSetting up swapspace version 1, size = 4 GiBand a UUID. -
Enable it and confirm.
sudo swapon /swapfile swapon --showExpected:
swapon --shownow lists/swapfilewithTYPE fileand the correctSIZE. -
Make the swap file permanent. Add an
/etc/fstabentry so it comes back after a reboot.echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabVerify the fstab is parseable before you trust it:
sudo swapoff /swapfile && sudo swapon -a && swapon --showExpected:
swapon -areads fstab and re-enables/swapfilewith no error. -
Set swappiness live. For most servers a value of
10is a reasonable starting point.sudo sysctl vm.swappiness=10Expected: the command echoes
vm.swappiness = 10. -
Persist swappiness and cache pressure. Live
sysctlchanges are lost on reboot; write them to a drop-in file under/etc/sysctl.d/.printf 'vm.swappiness=10\nvm.vfs_cache_pressure=100\n' | sudo tee /etc/sysctl.d/99-swap.confThen apply every drop-in:
sudo sysctl --systemExpected:
sysctl --systemprints the files it reads, including* Applying /etc/sysctl.d/99-swap.conf ..., followed byvm.swappiness = 10andvm.vfs_cache_pressure = 100. -
Watch pressure under load. Run
vmstat 1and read thesi(swap-in) andso(swap-out) columns while the workload runs. In a second terminal read the PSI file.vmstat 1 cat /proc/pressure/memoryExpected:
si/sosit at or near0unless RAM is genuinely exhausted; PSI showssome avg10=0.00(or a low number) when there is no memory stall.
Validate it works
swapon --showlists your swap device with the expected size and type.sysctl vm.swappinessreturns10(or whatever you set), and the value survivessudo rebootbecause it lives in/etc/sysctl.d/99-swap.conf.- Under representative load,
vmstat 1keepssi/soat or near zero. Sustained non-zero values mean the box is short on RAM, not that swappiness is wrong. /proc/pressure/memoryshows a lowsome avg10. Afull avg10above zero means tasks are stalling on memory and you should investigate RAM sizing, not just the dial.
Troubleshooting
swapon: /swapfile: insecure permissions 0644, 0600 suggested. The swap file is world-readable. Fix it withsudo chmod 0600 /swapfile, thensudo swapon /swapfileagain.swaponrefuses to attach a swap file that other users could read.- Swappiness reverts to 60 after reboot. The value was only set live, or it lives in a file that is not read. Confirm the drop-in exists and is applied:
cat /etc/sysctl.d/99-swap.confthensysctl vm.swappiness. A stray setting in/etc/sysctl.confor a lexically later file in/etc/sysctl.d/can also override yours — files are read in order, and the last one wins. - Swap is heavily used even though swappiness is low. Low swappiness discourages swapping; it cannot conjure RAM. Heavy swap use at low swappiness means the machine is genuinely out of memory. The fix is more RAM or less resident memory (fewer workers, smaller caches), not a lower dial. Confirm with
free -h— ifavailableis near zero, that is the real problem. - Database latency spikes that correlate with any swapping. For latency-sensitive engines (Postgres, MySQL, Redis) even a little swapping of hot pages shows up as tail-latency spikes. Set swappiness very low (
1) and, more importantly, make sure the working set fits in RAM. Watchsi/soduring the spike window to confirm swap is the cause. - Consider zram instead of disk swap. On memory-constrained hosts,
zramprovides a compressed swap device in RAM, trading a little CPU for far lower swap latency than disk. It is not a substitute for enough RAM, but it softens spikes better than a disk swap file. It is configured separately (zramctl/ a distro service), not through swappiness. - OOM killer fires with swap disabled. With no swap, the moment anonymous demand exceeds RAM the kernel invokes the OOM killer. If you see processes killed and
swapon --showis empty, either add swap to give the kernel headroom or reduce memory demand. Checkdmesg | grep -i oomfor the victims.
When this is the wrong approach
Swap tuning manages the symptom of memory pressure; it rarely fixes the cause. If a server swaps regularly, the honest fix is usually right-sizing RAM, not enlarging swap and hoping the dial hides it. For latency-critical databases, the goal is to keep the working set resident: set swappiness very low, size RAM to the data, and monitor si/so and PSI — adding more swap is the wrong instinct there. And on containerized or Kubernetes hosts, swap is frequently disabled by the platform entirely (the kubelet historically required it off), so tuning swappiness inside a pod may do nothing; adjust node-level memory requests and limits instead. See also Too many open files: raising ulimits safely and Diagnose a disk I/O bottleneck with iostat for the neighbouring limits you will hit once memory is sorted. The kernel’s own reference for these knobs is the vm sysctl documentation.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.