Skip to content
shellhacks.dev
Linux

How to Check and Change Swappiness on Linux Step by Step

Check and change Linux swappiness with sysctl, make it persistent, understand what the value 0 to 200 means, and learn when changing it does not help.

6 min read
Summarize in AI
Illustration for "How to Check and Change Swappiness on Linux Step by Step": a terminal session, gears, file permission bits.

Swap in use while memory is still free is one of the most asked Linux questions, and swappiness gets blamed for it. The setting is real and tunable, though it matters less than most guides claim. It sits next to the cache behaviour explained in the Linux page cache. The commands assume a Linux 6.x kernel and procps-ng 4.

Read the current value

Shell
cat /proc/sys/vm/swappiness
sysctl vm.swappiness
free -h
swapon --show
Text
60
vm.swappiness = 60
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       2.1Gi       3.4Gi       112Mi       2.2Gi       5.3Gi
Swap:          2.0Gi       180Mi       1.8Gi
NAME       TYPE SIZE   USED PRIO
/swapfile  file   2G 180M   -2

The value 60 is the kernel default. Some swap in use alongside free RAM is normal: the kernel moves rarely used pages out so the memory can hold cache that is used more. Look at available, not free, when judging pressure.

Change it now and keep it

Shell
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

The first command takes effect immediately and is lost at reboot. The file under /etc/sysctl.d/ is read at boot and by sysctl --system. Use a high-numbered file name so it overrides vendor defaults, which load in lexical order.

What the number means

The setting is the relative cost the kernel assigns to reclaiming anonymous memory (swap) versus file-backed pages (the cache). Since Linux 5.8 the range is 0 to 200, where 100 means equal cost and values above 100 favour swapping, which suits fast zram or SSD swap. A value of 10 keeps application memory resident longer and drops cache first, which suits a database host with plenty of RAM. A value of 0 does not turn swap off. The kernel documentation for vm.swappiness states the behaviour precisely, and to remove swap use swapoff instead.

Check whether it is actually swapping

Shell
vmstat 1 5
Text
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 184320 3560000  91000 2300000    0    0     4     8  210  390  3  1 96  0  0

The columns si and so show pages swapped in and out per second. Steady zeros mean the swap used is cold data and no problem. Sustained non-zero values with high wa (time waiting on disk) mean the machine is thrashing, and no swappiness value will fix a working set larger than RAM. Find the memory hog with the methods in measuring memory usage.

What swappiness controls

The kernel keeps two kinds of memory pages that it can reclaim: file-backed pages in the page cache, which can be dropped and re-read from disk, and anonymous pages, which hold program data and can only be freed by writing them to swap. The swappiness value, from 0 to 200 on recent kernels, sets the balance in how aggressively the kernel prefers to reclaim one kind over the other when memory is tight. A low value favours dropping cache and keeping program memory in RAM. A high value is more willing to push idle program pages to swap to keep more cache. The default of 60 is a general-purpose compromise.

It is not a percentage of memory at which swapping starts, which is a common misreading. Even at 0, the kernel will swap when it has no other way to free memory. And at the default, a system with plenty of free memory does not swap at all.

Workloads and sensible values

Database servers and latency-sensitive services are often set to a low value, commonly 1 to 10, so their working set stays in memory and an idle page is not evicted just before it is needed. The trade-off is less cache for file reads, which matters little when the database manages its own cache. Desktop systems with limited memory can benefit from the default or a slightly higher value, since swapping out idle application pages keeps the interface responsive when a large file is open. Systems using compressed memory swap, such as zram or zswap, often work better with a high value, because swapping to compressed RAM is far cheaper than disk IO.

Do not set the value to 0 on the belief that it disables swap. It reduces the tendency, nothing more, and a machine without enough memory will still hit the out-of-memory killer. To turn swap off, remove the swap devices.

Making a change persistent

Changing the value live with sysctl vm.swappiness=10 lasts until reboot. To keep it, add a file in /etc/sysctl.d/ with the line vm.swappiness = 10 and apply it with sysctl --system. Using a drop-in file, not editing the main configuration, survives package upgrades and documents the change in one place with a comment naming the reason. In containers, the value belongs to the host kernel and cannot be set per container, although cgroup version 2 offers memory controls like memory.swap.max that limit swap use per workload, which is the better tool for isolating a noisy neighbour.

Record why the value was chosen. A figure copied from a forum, with no note, is the kind of tuning that nobody dares remove years later.

Judging whether it helped

Change one thing at a time and measure under the real workload. The columns si and so in vmstat 1 show swap in and out per second, and sustained non-zero values signal that the system is paging actively, which hurts latency. Resting swap use, in contrast, is normal: a few hundred megabytes of idle pages in swap is the kernel doing its job. Look at available memory, not free memory, since the kernel intentionally fills spare RAM with cache. If a machine swaps heavily, the cure is usually more memory, a smaller working set or a lower limit on a runaway process, and no swappiness value fixes a workload that does not fit.

Related settings sit next to swappiness. The value of vm.vfs_cache_pressure controls how readily the kernel reclaims directory and inode caches, and vm.dirty_ratio controls when writers are throttled. Changing several at once makes it impossible to know which helped. Keep a log of each change with the date, the symptom and the measurement before and after, and revert anything that did not show a clear gain. Most systems run well on defaults, and tuning is justified only by a measured problem.

On machines with no swap at all, the setting has no effect, and a memory spike goes straight to the out-of-memory killer. A small swap area, or zram, is cheap insurance that gives the kernel room to move idle pages.

Where this does not apply

On containers, swappiness is a host setting, and cgroup v2 limits swap per container with memory.swap.max. On a laptop that hibernates, the swap partition must stay large enough for the RAM image. Tuning is no substitute for capacity: adding RAM, fixing a leak or limiting a service with a cgroup beats a sysctl change. If swap lives on a slow disk, consider zram instead, as covered by the Arch wiki entry on zram. Check disk health with smartctl if swap disk IO is unusually slow.

Taggedlinux
Share

Keep reading

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.