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

the swap question, and why zram quietly won me over

Where I landed on swap for my low-RAM homelab nodes after trying zram, and why a compressed swap in RAM beats both no swap and disk swap.

A rack of small servers with status lights

The swap debate flares up again every time someone with 64GB of RAM declares it pointless. They are not wrong for their machine. They are wrong for mine, because half my homelab is small nodes with 1 or 2GB, and on those swap is the difference between a service surviving a memory spike and the OOM killer culling something at random.

On the tiny nodes I stopped using a disk swap partition and switched to zram: a compressed block device living in RAM that you swap to. It sounds daft, swapping RAM to RAM, but text and many in-memory structures compress two or three to one, so you effectively reclaim memory at the cost of a little CPU. Set up is a few lines:

modprobe zram
echo lz4 > /sys/block/zram0/comp_algorithm
echo 512M > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 100 /dev/zram0

The high priority means the kernel reaches for zram before any disk swap. With vm.swappiness left around 10, idle pages get compressed and tucked away, and the SD card or spinning disk never wakes up for paging. No thrash, no flash wear, and the box rides out the spike that would otherwise have killed a daemon.

So that is where the debate ends for me. Big boxes: a small disk swap and a low swappiness, as ever. Tiny boxes: zram, because RAM you can compress is cheaper than RAM you have to buy.