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

how much swap, then? what i actually do on my own boxes

A pragmatic position on the perennial Linux swap question for a homelab, why "disable swap entirely" is usually wrong, and the small amount plus swappiness tweak I settle on for my own machines.

A terminal showing free -h and vmstat output on a Linux server

Every so often the swap argument comes round again, usually started by someone who read that swap is "slow disk pretending to be RAM" and concluded the answer is to turn it off. I've held most positions on this over the years, including the wrong ones, so here is where I've actually landed for my own homelab boxes, with reasons rather than dogma.

The short version: a little swap, on, with low swappiness. Not zero, not "twice your RAM" out of 2004 habit, just a modest amount that the kernel can use as a pressure valve.

The case people make for disabling swap entirely is that they don't want their machine grinding to a halt paging to disk when memory gets tight. Fair. Nobody enjoys a box that's technically alive but thrashing so hard you can't even SSH in to kill the offender. But disabling swap doesn't fix that, it just changes how the failure looks. With no swap, when memory runs out the OOM killer wakes up and shoots a process, possibly the wrong one, possibly the database you cared about, with no warning and no graceful slope before the cliff. You've traded "slow" for "sudden", and sudden is harder to recover from.

free -h showing a small swap partition lightly used

A small amount of swap gives the kernel somewhere to put genuinely cold pages, the stuff that gets allocated once at startup and never touched again, so that actual hot memory has more room. That's the bit people miss: swap isn't only an emergency overflow, it's a place to evict pages that have no business occupying RAM. On a box running a handful of long-lived services, there's always a fair amount of that.

The tuning that matters more than the size is swappiness. The default of 60 is tuned for desktops and is too eager for a server where I'd rather keep things in RAM and only swap under real pressure:

# check current
cat /proc/sys/vm/swappiness

# lower it, and make it stick
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

At vm.swappiness=10 the kernel strongly prefers to reclaim cache before it touches application memory, but swap is still there as a backstop. That's the behaviour I want: stay in RAM in normal operation, lean on swap only when genuinely squeezed, and avoid handing the OOM killer a hair trigger.

So, settled, for me at least: a couple of gigabytes of swap on each box, swappiness down at 10, and the OOM killer left as the last resort it's meant to be rather than the first responder. The boxes that run leanest aren't the ones with swap disabled. They're the ones where swap exists, sits nearly empty most of the time, and quietly does its job on the one afternoon a month something tries to eat all the memory at once.