Ramblings of an aging IT geek
← Ramblings of an aging IT geek
linux

i finally made my peace with swap

Why I keep a small swap file on every homelab box and tune swappiness down instead of disabling swap entirely.

A Linux terminal showing memory and swap usage

The swap argument never really ends. One camp says "you have plenty of RAM, turn swap off". The other says "you must have swap equal to RAM for hibernation". Both are answering a question I don't have. For my homelab the answer turned out to be boring and in the middle, and I've stopped fretting about it.

I keep a small swap file on every box. Not gigabytes to match RAM, just one or two gig. The point isn't to extend memory, it's to give the kernel somewhere to park genuinely cold pages so it can spend that RAM on page cache instead. Disable swap entirely and you don't make your machine faster; you just remove an option the kernel was using sensibly, and you make the OOM killer the first line of defence rather than the last.

What I did change is vm.swappiness. The default of 60 is tuned for desktops and it'll swap out things I'd rather it didn't. On a server I want it reluctant:

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

At 10 the kernel only swaps when it's actually under pressure, which is exactly the moment I want it to have the option. The one place I genuinely turn swap off is inside Kubernetes nodes, because the kubelet expects it gone and fights you otherwise.

So that's it, settled, at least for me. Small swap file, low swappiness, move on. It's not a thrilling conclusion, but the thrilling conclusions in this debate usually come from people who measured their laptop and extrapolated to a fleet.