Difference between revisions of "Linux swap space"

From Christoph's Personal Wiki
Jump to: navigation, search
(Creating a swap file)
 
(2 intermediate revisions by the same user not shown)
Line 32: Line 32:
 
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 # echo 1048576/1024^2|bc -l => 1.00
  
 
* Set the right permissions (a world-readable swap file is a huge local vulnerability):
 
* Set the right permissions (a world-readable swap file is a huge local vulnerability):
Line 50: Line 50:
 
  $ /mnt/1GB.swap  none  swap  defaults  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> (see: [[sysctl]]) and add the following line:
  
 
  vm.swappiness=10
 
  vm.swappiness=10
Line 58: Line 58:
 
*Check that the swap file was created:
 
*Check that the swap file was created:
  
  $ sudo swapon -s
+
  $ 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. Or, just enable with:
 +
$ swapon -a
  
 
==swappiness==
 
==swappiness==

Latest revision as of 02:16, 14 January 2016

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 (see: sysctl) 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.