Linux swap space

From Christoph's Personal Wiki
Revision as of 04:56, 30 October 2013 by Christoph (Talk | contribs) (New page: In Linux, '''swap''' is space on the hard disk that is reserved to be used as virtual memory. ==swappiness== How aggressively your computer uses its swap space is determined by the va...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In Linux, swap is space on the hard disk that is reserved to be used as virtual memory.

swappiness

How aggressively your computer uses its swap space is determined by the value for "swappiness", which is in a range of 0 to 100. A setting of 100 will aggressively move processes, while a setting of 0 will swap only to avoid an out of memory condition.

Creating a swap file

As an example, to add 1GB of swap to your computer/server, follow these steps (as superuser/root):

  • Create the file to be used for swap:
fallocate -l 1G /mnt/1GB.swap

Or, if fallocate fails or is not installed, run the following command:

dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
  • Format the file for swap:
mkswap /mnt/1GB.swap
  • Add the file to the system as a swap file:
swapon /mnt/1GB.swap
  • Add the following line to your /etc/fstab file to make the change permanent:
/mnt/1GB.swap  none  swap  sw  0  0
  • To change the swappiness edit /etc/sysctl.conf and add the following line:
vm.swappiness=10

Start with a value of 10 and increase if needed. A typical default value for swappiness is 60. The higher the number, the more often swap is utilized. How much this affects performance depends on how your memory is being used, so experiment to find an optimal value.

  • Check that the swap file was created:
sudo swapon -s

Finally, reboot your computer/server to ensure that the changes go into effect.