I finally got sick of linux (Ubuntu and Centos to be more precise) swapping all the time when I have 8GB and 16GB of memory in the two machines I use all the time. So I decided that it was time I learnt about how to tune the swappiness. Turns out that it is particularly simple.
Swappiness is a number between 0 and 100. 0 means swap only to prevent an out of memory condition, 100 means go nuts and swap aggressively. On both of my machines it was set to 60.
To see what value your system currently has
- cat /proc/sys/vm/swappiness
To temporarily set the swappiness to something sane, say 10,
- sudo sysctl -w vm.swappiness=10
To make this value persist across reboots, edit /etc/sysctl.conf as root and insert