Speed up your system by avoiding the swap file

  • February 8, 2009
  • Avatar for jacob

Most modern operating systems are capable of using a file or partition known as a swap or paging file. Most Linux distributions will also install one for you by default. This file is used to extend the amount of available RAM by writing some of it to your hard drive.

There's just one problem: hard drives are slow. We can't fix that problem yet, but we can avoid it. The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness. A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly.

My Ubuntu system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:

cat /proc/sys/vm/swappiness

My HDD is insanely slow and I have 2 GB of RAM, so I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent. To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 15

Reboot for the change to take effect. You can also change the value while your system is still running:

sysctl vm.swappiness=15

However, you won't get the full effect of rebooting because there is probably already memory stored in swap that won't instantly be moved out.

Update: Readers have noted that you can clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

Avatar for jacob Jacob Peddicord

Home » Articles »