Difference between revisions of "Linux swap space"

From Christoph's Personal Wiki
Jump to: navigation, search
(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...)
 
Line 1: Line 1:
 
In [[Linux]], '''swap''' is space on the hard disk that is reserved to be used as virtual memory.
 
In [[Linux]], '''swap''' is space on the hard disk that is reserved to be used as virtual memory.
  
==swappiness==
+
==Swap partition==
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.
+
 
 +
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 <code>/etc/fstab</code>:
 +
 
 +
/dev/sda2 none swap defaults 0 0
  
 
==Creating a swap file==
 
==Creating a swap file==
Line 9: Line 28:
 
*Create the file to be used for swap:
 
*Create the file to be used for swap:
  
  fallocate -l 1G /mnt/1GB.swap
+
  $ fallocate -l 1G /mnt/1GB.swap
  
 
Or, if <code>fallocate</code> fails or is not installed, run the following command:
 
Or, if <code>fallocate</code> fails or is not installed, run the following command:
  
  dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
+
  $ dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
 +
 
 +
* Set the right permissions (a world-readable swap file is a huge local vulnerability):
 +
 
 +
$ chmod 600 /swapfile
  
 
*Format the file for swap:
 
*Format the file for swap:
  
  mkswap /mnt/1GB.swap
+
  $ mkswap /mnt/1GB.swap
  
 
*Add the file to the system as a swap file:
 
*Add the file to the system as a swap file:
  
  swapon /mnt/1GB.swap
+
  $ swapon /mnt/1GB.swap
  
 
*Add the following line to your <code>/etc/fstab</code> file to make the change permanent:
 
*Add the following line to your <code>/etc/fstab</code> file to make the change permanent:
  
  /mnt/1GB.swap  none  swap  sw 0  0
+
  $ /mnt/1GB.swap  none  swap  defaults 0  0
  
 
*To change the swappiness edit <code>/etc/sysctl.conf</code> and add the following line:
 
*To change the swappiness edit <code>/etc/sysctl.conf</code> and add the following line:
Line 35: Line 58:
 
*Check that the swap file was created:
 
*Check that the swap file was created:
  
  sudo swapon -s
+
  $ sudo swapon -s
  
 
Finally, reboot your computer/server to ensure that the changes go into effect.
 
Finally, reboot your computer/server to ensure that the changes go into effect.
 +
 +
==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 <code>/etc/sysctl.conf</code> 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 <code>swapoff -a</code> and then <code>swapon -a</code> as root instead of rebooting to achieve the same effect.
  
 
[[Category:Linux Command Line Tools]]
 
[[Category:Linux Command Line Tools]]

Revision as of 20:12, 11 October 2014

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
  • 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:
$ sudo swapon -s

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

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.