This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files.
- 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.
Support for swap is provided by the Linux kernel and user-space utilities from thepackage.
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 --show
$ free -h
A swap partition can be created with most GNU/Linux partitioning tools. Swap partitions are typically designated as type
82. Even though it is possible to use any partition type as swap, it is recommended to use type
82 in most cases since systemd will automatically detect it and mount it (see below).
To set up a partition as Linux swap area, the
mkswap command is used. For example:
# mkswap /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
- Adding an entry to fstab is optional in most cases with systemd. See the next subsection.
- If using an SSD with TRIM support, consider using
defaults,discardin the swap line in fstab. If activating swap manually with swapon, using the
--discardparameter achieves the same. See
man 8 swaponfor details.
Activation by systemd
systemd activates swap partitions based on two different mechanisms. Both are executables in
/usr/lib/systemd/system-generators. The generators are run on start-up and create native systemd units for mounts. The first,
systemd-fstab-generator, reads the fstab to generate units, including a unit for swap. The second,
systemd-gpt-auto-generator inspects the root disk to generate units. It operates on GPT disks only, and can identify swap partitions by their type code
This can be solved by one of the following options:
- Removing the swap entry from
- Changing the swap partition's type code from
82to an arbitrary type code
- Setting the attribute of the swap partition to "63: do not automount"
To deactivate specific swap space:
# swapoff /dev/sda2
Alternatively use the
-a switch to deactivate all swap space.
Since swap is managed by systemd, it will be activated again on the next system startup. To disable the automatic activation of detected swap space permanently, run
systemctl --type swap to find the responsible .swap unit and mask it.
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). For example, creating a 512 MB swap file:
# fallocate -l 512M /swapfile
# 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
/swapfile none swap defaults 0 0
Remove swap file
To remove a swap file, the current swap file must be turned off.
# swapoff -a
Remove swap file:
# rm -f /swapfile
Finally remove the relevant entry from
Swap with USB device
Thanks to the modularity offered by Linux, we can have multiple swap partitions spread over different devices. If you have a very full hard disk, a USB device can be used as a swap partition temporarily. However, this method has some severe disadvantages:
- A USB device is slower than a hard disk
- Flash memory has a limited number of write cycles. Using it as a swap partition can kill it quickly
To add a USB device to swap, first take a USB flash drive and partition it for swap as described in #Swap partition.
Now add the following 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 the UUID is taken from the command:
ls -l /dev/disk/by-uuid/ | grep /dev/sdc1
Just replace sdc1 with your new USB swap partition.
in the original swap entry so that the USB swap partition will take priority over the old swap partition.
This guide will work for other memory such as SD cards, etc.
Swap values can be adjusted to help performance.
The swappiness sysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100, the default value is 60. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.
To check the current swappiness value:
$ cat /proc/sys/vm/swappiness
To temporarily set the swappiness value:
# sysctl vm.swappiness=10
To set the swappiness value permanently, edit a sysctl configuration file
To test and more on why this may work, take a look at this article.
Another sysctl parameter that affects swap performance is
vm.vfs_cache_pressure, which controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches, versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed. For more information, see the Linux kernel documentation.
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
/dev/sda1 none swap defaults,pri=100 0 0 /dev/sdb2 none swap defaults,pri=10 0 0
Or via the
--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.
There is no necessity to use RAID for swap performance reasons. The kernel itself can stripe swapping on several devices, if you just give them the same priority in the
/etc/fstab file. Refer to The Software-RAID HOWTO for details.