← Back

On Swap

I used to think the physical memory on Alibaba Cloud’s lightweight servers was too small and that I should raise the swap size.

Testing showed the opposite: more swap saturated disk I/O and froze the whole machine. It would take half an hour to an hour to respond again, or I had to force a shutdown and reboot.

The real culprit is swappiness.

Alibaba Cloud’s lightweight servers default to 60. Memory is far faster than disk, so a high value increases system I/O and causes a lot of pages to be swapped in and out, which badly hurts performance. At the OS level we want to use memory as much as possible, so this parameter needs adjusting.

Setting it to 1 is enough.

sysctl vm.swappiness=1
echo "vm.swappiness=1" >> /etc/sysctl.conf