Difference between revisions of "Swap"
(adding link to french version) |
m (→Priority) |
||
(35 intermediate revisions by 17 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category: | + | [[Category:File systems]] |
+ | [[es:Swap]] | ||
+ | [[fr:Swap]] | ||
+ | [[it:Swap]] | ||
+ | [[ja:Swap]] | ||
[[pt:Swap]] | [[pt:Swap]] | ||
[[zh-CN:Swap]] | [[zh-CN:Swap]] | ||
− | + | {{Related articles start}} | |
− | {{ | + | {{Related|Swap on video ram}} |
− | {{ | + | {{Related|fstab}} |
− | + | {{Related articles end}} | |
− | + | This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files. | |
− | |||
− | |||
From [http://www.linux.com/news/software/applications/8208-all-about-linux-swap-space All about Linux swap space]: | From [http://www.linux.com/news/software/applications/8208-all-about-linux-swap-space All about Linux swap space]: | ||
− | :''Linux divides its physical RAM (random access memory) into | + | :''Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.'' |
== Swap space == | == Swap space == | ||
Line 20: | Line 22: | ||
$ swapon -s | $ swapon -s | ||
− | + | Or: | |
$ free -m | $ free -m | ||
+ | |||
+ | {{Note|There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.}} | ||
== Swap partition == | == Swap partition == | ||
− | A swap partition can be created with most GNU/Linux partitioning tools (e.g. {{ | + | A swap partition can be created with most GNU/Linux partitioning tools (e.g. {{ic|fdisk}}, {{ic|cfdisk}}). Swap partitions are designated as type '''82'''. |
− | To set up a Linux swap area, the {{ | + | To set up a Linux swap area, the {{ic|mkswap}} command is used. For example: |
# mkswap /dev/sda2 | # mkswap /dev/sda2 | ||
{{Warning|All data on the specified partition will be lost.}} | {{Warning|All data on the specified partition will be lost.}} | ||
+ | |||
+ | The ''mkswap'' utility generates an UUID for the partition by default, use the {{ic|-U}} flag in case you want to specify custom UUID: | ||
+ | # mkswap -U ''custom_UUID'' /dev/sda2 | ||
To enable the device for paging: | To enable the device for paging: | ||
Line 37: | Line 44: | ||
To enable this swap partition on boot, add an entry to [[fstab]]: | To enable this swap partition on boot, add an entry to [[fstab]]: | ||
/dev/sda2 none swap defaults 0 0 | /dev/sda2 none swap defaults 0 0 | ||
+ | |||
+ | {{Note|If using an SSD with TRIM support, consider using {{ic|defaults,discard}} in the swap line in [[fstab]]. If activating swap manually with ''swapon'', using the {{ic|-d}} or {{ic|--discard}} parameter achieves the same. See {{ic|man 8 swapon}} for details.}} | ||
== Swap file == | == Swap file == | ||
Line 42: | Line 51: | ||
As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD). | As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD). | ||
− | {{Note|The | + | {{Note|The Btrfs file system does not currently support swap files. Failure to heed this warning may result in file system corruption. Though it should be noted that one may use a swap file on Btrfs if mounted through a loop device. This method will result in severely degraded swap performance.}} |
=== Swap file creation === | === Swap file creation === | ||
− | As root use {{ | + | As root use {{ic|fallocate}} to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) ({{Ic|dd}} can also be used but will take longer). For example, creating a 512 MB swap file: |
# fallocate -l 512M /swapfile | # fallocate -l 512M /swapfile | ||
+ | Or | ||
# dd if=/dev/zero of=/swapfile bs=1M count=512 | # dd if=/dev/zero of=/swapfile bs=1M count=512 | ||
Line 55: | Line 65: | ||
# chmod 600 /swapfile | # chmod 600 /swapfile | ||
− | After creating the correctly | + | After creating the correctly sized file, format it to swap: |
# mkswap /swapfile | # mkswap /swapfile | ||
− | Activate the | + | Activate the swap file: |
# swapon /swapfile | # swapon /swapfile | ||
Line 68: | Line 78: | ||
=== Remove swap file === | === Remove swap file === | ||
+ | |||
+ | {{Warning| swap is managed by systemd and will be reenabled by it after some time. }} | ||
To remove a swap file, the current swap file must be turned off. | To remove a swap file, the current swap file must be turned off. | ||
Line 75: | Line 87: | ||
# swapoff -a | # swapoff -a | ||
− | Remove | + | Remove swap file: |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | # rm -f /swapfile | |
− | |||
== Swap with USB device == | == Swap with USB device == | ||
Line 103: | Line 98: | ||
* when another device is attached to the computer, no swap can be used. | * when another device is attached to the computer, no swap can be used. | ||
− | To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table. | + | To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table. |
− | |||
− | + | {{Warning|Make sure you are writing the partition to the correct disk!}} | |
− | + | Next open {{ic|/etc/fstab}}. | |
Now add a new entry, just under the current swap entry, which take the current swap partition over the new USB one | Now add a new entry, just under the current swap entry, which take the current swap partition over the new USB one | ||
Line 118: | Line 112: | ||
ls -l /dev/disk/by-uuid/ | grep /dev/sdc1 | ls -l /dev/disk/by-uuid/ | grep /dev/sdc1 | ||
− | Just replace sdc1 with your new USB swap partition. {{ | + | Just replace sdc1 with your new USB swap partition. {{ic|sdb1}} |
− | {{ | + | {{Tip|We use UUID because when you attach other devices to the computer it could modify the device order}} |
Last, add | Last, add | ||
Line 138: | Line 132: | ||
The ''swappiness'' [[sysctl]] parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems. | The ''swappiness'' [[sysctl]] parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems. | ||
− | {{hc|/etc/sysctl.conf | + | {{hc|/etc/sysctl.d/99-sysctl.conf|2= |
− | |2= | ||
vm.swappiness=1 | vm.swappiness=1 | ||
vm.vfs_cache_pressure=50 | vm.vfs_cache_pressure=50 | ||
− | + | }} | |
+ | |||
+ | To test and more on why this may work, take a look at this [http://rudd-o.com/en/linux-and-free-software/tales-from-responsivenessland-why-linux-feels-slow-and-how-to-fix-that article]. | ||
+ | |||
+ | [http://askubuntu.com/questions/103915/how-do-i-configure-swappiness This] Q&A post explains a lot about swappiness. | ||
=== Priority === | === Priority === | ||
− | If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. | + | If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ({{ic|/dev/sda}}) and a slower disk ({{ic|/dev/sdb}}), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the {{ic|1=pri}} parameter: |
/dev/sda1 none swap defaults,pri=100 0 0 | /dev/sda1 none swap defaults,pri=100 0 0 | ||
/dev/sdb2 none swap defaults,pri=10 0 0 | /dev/sdb2 none swap defaults,pri=10 0 0 | ||
− | Or via the {{ | + | Or via the {{ic|-p}} (or {{ic|--priority}}) parameter of swapon: |
# swapon -p 100 /dev/sda1 | # swapon -p 100 /dev/sda1 | ||
If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them. | If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them. |
Revision as of 16:44, 31 January 2014
This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files.
From All about Linux swap space:
- Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.
Contents
Swap space
Swap space will usually be a disk partition but can also be a file. Users may create a swap space during installation of Arch Linux or at any later time should it become necessary. Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).
To check swap status, use:
$ swapon -s
Or:
$ free -m
Swap partition
A swap partition can be created with most GNU/Linux partitioning tools (e.g. fdisk
, cfdisk
). Swap partitions are designated as type 82.
To set up a Linux swap area, the mkswap
command is used. For example:
# mkswap /dev/sda2
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
To enable the device for paging:
# swapon /dev/sda2
To enable this swap partition on boot, add an entry to fstab:
/dev/sda2 none swap defaults 0 0
defaults,discard
in the swap line in fstab. If activating swap manually with swapon, using the -d
or --discard
parameter achieves the same. See man 8 swapon
for details.Swap file
As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).
Swap file creation
As root use fallocate
to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd
can also be used but will take longer). For example, creating a 512 MB swap file:
# fallocate -l 512M /swapfile Or # dd if=/dev/zero of=/swapfile bs=1M count=512
Set the right permissions (a world-readable swap file is a huge local vulnerability)
# chmod 600 /swapfile
After creating the correctly sized file, format it to swap:
# mkswap /swapfile
Activate the swap file:
# swapon /swapfile
Edit /etc/fstab
and add an entry for the swap file:
/swapfile none swap defaults 0 0
Remove swap file
To remove a swap file, the current swap file must be turned off.
As root:
# swapoff -a
Remove swap file:
# rm -f /swapfile
Swap with USB device
Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. If you have a very full hard disk, USB device can be used as partition temporally. But this method has some severe disadvantage:
- USB device is slower than hard disk.
- flash memories have limited write cycles. Using it as swap partition will kill it quickly.
- when another device is attached to the computer, no swap can be used.
To add a a USB device to SWAP, first take a USB flash and partition it with a swap partition.You can use graphical tools such as Gparted or console tools like fdisk. Make sure to label the partition as SWAP before writing the partition table.
Next open /etc/fstab
.
Now add a new entry, just under the current swap entry, which take the current swap partition over the new USB one
UUID=... none swap defaults,pri=10 0 0
where UUID is taken from the output of the command
ls -l /dev/disk/by-uuid/ | grep /dev/sdc1
Just replace sdc1 with your new USB swap partition. sdb1
Last, add
pri=0
in the original swap entry for teaching fstab to use HD swap only when USB is full
This guide will work for other memory such as SD cards, etc.
Performance Tuning
Swap values can be adjusted to help performance.
Swappiness
The swappiness sysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.
/etc/sysctl.d/99-sysctl.conf
vm.swappiness=1 vm.vfs_cache_pressure=50
To test and more on why this may work, take a look at this article.
This Q&A post explains a lot about swappiness.
Priority
If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk (/dev/sda
) and a slower disk (/dev/sdb
), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the pri
parameter:
/dev/sda1 none swap defaults,pri=100 0 0 /dev/sdb2 none swap defaults,pri=10 0 0
Or via the -p
(or --priority
) parameter of swapon:
# swapon -p 100 /dev/sda1
If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.