brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: vm.swappiness tells linux how expensive swap is compared with the page cache

Linux
~/tips/tip-performance-vm-swappiness

sysctl vm.swappiness

Sets how eagerly linux swaps out memory compared with dropping page cache.

Lowering vm.swappiness is a tweak I see passed around a lot. The kernel documentation says something more careful, and I find what the number actually means more useful than the folk rule.

Read the current value, and change it for this boot:

sysctl vm.swappiness
sudo sysctl -w vm.swappiness=30

The docs describe it as the rough relative IO cost of swapping and of filesystem paging, a value between 0 and 200. At 100 the kernel assumes both cost the same and puts memory pressure on the page cache and on swap-backed pages equally. Lower values mean swap IO is more expensive, higher values mean it is cheaper. The default is 60.

That is why values above 100 exist. For in-memory swap such as zram or zswap, or swap on a device that is faster than the filesystem, the docs say values beyond 100 can be considered. Their example: if random IO on the swap device is on average twice as fast as filesystem IO, swappiness should be 133.

The caveat is that there is no universal best number. The docs point out that filesystem IO under memory pressure tends to be more efficient than the random IO of swap, and that the right value needs experimentation and depends on the workload. To keep a value across boots, put it in a .conf file in /etc/sysctl.d/, for example vm.swappiness = 30 in 99-swappiness.conf. The sysctl.d manual says systemd reads those files at boot to configure the kernel parameters. And before you change anything, look at the pressure files in /proc/pressure to see whether memory is a problem at all.

Further reading: Documentation for /proc/sys/vm (Linux kernel docs), sysctl.d(5) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link