netarky.com

Linux swap

Updated 28 Feb 2014

Linux swap * Swap partition sizing * Swappiness

 

Linux swap

Linux has two forms of swap space: the swap partition and the swap file. The swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. The swap file is a special file in the filesystem that resides amongst your system and data files.

You will recognise the swap partition if you installed Linux and configured the disk partitions yourself. Almost all Linux installations will have a swap partition on the hard disk.

Linux also supports a swap file that you can create, prepare, and mount in a fashion similar to that of a swap partition. The advantage of swap files is that you don't need to find an empty partition or repartition a disk to add additional swap space.

To see what swap space you have, use the command swapon -s.

 

Swap partition sizing

When creating a swap partition, a rule of thumb is as follows:

  1. for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications;
  2. for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary;
  3. for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB.

 

Swappiness

The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle.

The default value for swappiness is 60. If swappiness is much increased, application response time drops. This is because the system has to swap the recently activated application back from the hard disk into RAM. On the other hand, if swappiness is much reduced, you may end up with hundreds of megabytes of untouched RAM that could be used for other things.

The following temporarily sets the swappiness value to 10:

sudo sysctl -w vm.swappiness=10
 
# Verify the change
cat /proc/sys/vm/swappiness

Edit /etc/sysctl.conf to permanently change swappiness by adding this line:

vm.swappiness = 10

 

Sources:
Swappiness
All about Linux swap space