Linux swap space

From Christoph's Personal Wiki
Revision as of 05:43, 11 November 2014 by Christoph (Talk | contribs) (Creating a swap file)

Jump to: navigation, search

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

Swap partition

A swap partition can be created with most Linux partitioning tools (e.g. fdisk, cfdisk). Swap partitions are typically designated as type 82, however it is possible to use any partition type as swap.

  • Set up a Linux swap area:
$ mkswap /dev/sda2

Warning: All data on the specified partition will be lost.

The mkswap utility generates an UUID for the partition by default, use the -U flag in case you want to specify custom UUID:

$ mkswap -U custom_UUID /dev/sda2
  • Enable the device for paging:
$ swapon /dev/sda2
  • Enable this swap partition on boot by adding an entry to /etc/fstab:
/dev/sda2 none swap defaults 0 0

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  # echo 1048576/1024^2|bc -l => 1.00
  • Set the right permissions (a world-readable swap file is a huge local vulnerability):
$ chmod 600 /swapfile
  • 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  defaults  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:
$ swapon -s

Finally, reboot your computer/server to ensure that the changes go into effect. Or, just enable with:

$ swapon -a

swappiness

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory. 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.

  • Check the default swappiness value:
$ cat /proc/sys/vm/swappiness
  • To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:
vm.swappiness = 10

Reboot for the change to take effect.

You can also change the value while your system is still running:

$ sysctl vm.swappiness=10

Or, clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.