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).

+

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), a.k.a "enable swap", and also for suspend-to-disk support.

+

+

If it is beneficial to enable swap depends on the amount of installed physical memory, and the amount of memory required to run all the desired programs. If the amount of physical memory is less than the required amount, then it is beneficial to enable swap. This avoids [[Wikipedia:Out of memory|out of memory conditions]], where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference as swap space. For example, if your programs require 7.5 GB of memory to run, and there are 4 GB of physical memory installed, add the difference of 3.5 GB in swap space. Add more swap space to account for future requirements.

+

It is a matter of personal preference if you prefer programs to be killed over enabling swap. The biggest drawback to enabling swap is its lower performance, see section [[#Performance]].

To check swap status, use:

To check swap status, use:

$ swapon --show

$ swapon --show

−

Or:

+

Or

$ free -h

$ free -h

+

free also indicates if memory is running short, which can be remedied by enabling swap or increasing swap.

{{Note|There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.}}

{{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]]. Swap partitions are typically designated as type {{ic|82}}. Even though it is possible to use any partition type as swap, it is recommended to use type {{ic|82}} in most cases since [[systemd]] will automatically detect it and mount it (see below).

A swap partition can be created with most GNU/Linux [[partitioning tools]]. Swap partitions are typically designated as type {{ic|82}}. Even though it is possible to use any partition type as swap, it is recommended to use type {{ic|82}} in most cases since [[systemd]] will automatically detect it and mount it (see below).

Line 64:

Line 68:

{{Warning|Enabling discard on RAID setups using mdadm will cause system lockup on boot and during runtime, if using swapon.}}

{{Warning|Enabling discard on RAID setups using mdadm will cause system lockup on boot and during runtime, if using swapon.}}

−

==== Activation by systemd ====

+

=== Activation by systemd ===

−

[[systemd]] activates swap partitions based on two different mechanisms. Both are executables in {{ic|/usr/lib/systemd/system-generators}}. The generators are run on start-up and create native systemd units for mounts. The first, {{ic|systemd-fstab-generator}}, reads the fstab to generate units, including a unit for swap. The second, {{ic|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 {{ic|82}}.

+

[[systemd]] activates swap partitions based on two different mechanisms. Both are executables in {{ic|/usr/lib/systemd/system-generators}}. The generators are run on start-up and create native systemd units for mounts. The first, {{ic|systemd-fstab-generator}}, reads the fstab to generate units, including a unit for swap. The second, {{ic|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 GUID, see [[systemd#GPT partition automounting]] for more information.

−

==== Disabling swap ====

+

=== Disabling swap ===

To deactivate specific swap space:

To deactivate specific swap space:

Line 77:

Line 81:

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 {{ic|systemctl --type swap}} to find the responsible ''.swap'' unit and [[mask]] it.

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 {{ic|systemctl --type swap}} to find the responsible ''.swap'' unit and [[mask]] it.

−

=== Swap file ===

+

== 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).

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).

Line 83:

Line 87:

{{Warning|[[Btrfs]] does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.}}

{{Warning|[[Btrfs]] does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.}}

To remove a swap file, it must be turned off first and then can be removed:

# swapoff -a

# swapoff -a

−

−

Remove swap file:

−

# rm -f /swapfile

# rm -f /swapfile

Finally remove the relevant entry from {{ic|/etc/fstab}}.

Finally remove the relevant entry from {{ic|/etc/fstab}}.

+

+

=== Automated ===

==== systemd-swap ====

==== systemd-swap ====

−

[[Install]] the {{pkg|systemd-swap}} package. Set {{ic|1=swapfu_enabled=1}} in the ''Swap File Universal'' section of {{ic|/etc/systemd/swap.conf}}. [[Start/enable]] the {{ic|systemd-swap}} service. Visit the [https://github.com/Nefelim4ag/systemd-swap authors GitHub] page for more information and setting up the [https://github.com/Nefelim4ag/systemd-swap/blob/master/README.md#about-configuration recommend configuration].

+

[[Install]] the {{pkg|systemd-swap}} package. Set {{ic|1=swapfc_enabled=1}} in the ''Swap File Chunked'' section of {{ic|/etc/systemd/swap.conf}}. [[Start/enable]] the {{ic|systemd-swap}} service. Visit the [https://github.com/Nefelim4ag/systemd-swap authors GitHub] page for more information and setting up the [https://github.com/Nefelim4ag/systemd-swap/blob/master/README.md#about-configuration recommended configuration].

+

+

{{Note|If the journal keeps showing the following warning {{ic|systemd-swap[..]: WARN: swapFC: ENOSPC}} and no swap file is being created, you need to set {{ic|1=swapfc_force_preallocated=1}} in {{ic|/etc/systemd/swap.conf}}.}}

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:

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

* 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

* Flash memory has a limited number of write cycles. Using it as a swap partition can kill it quickly

Line 160:

Line 165:

To check the current swappiness value:

To check the current swappiness value:

+

+

$ cat /sys/fs/cgroup/memory/memory.swappiness

+

+

or

$ cat /proc/sys/vm/swappiness

$ cat /proc/sys/vm/swappiness

+

+

{{Note|As {{ic|/proc}} is a lot less organized and is kept only for compatibility purposes, you are encouraged to use {{ic|/sys}} instead.}}

To temporarily set the swappiness value:

To temporarily set the swappiness value:

Line 181:

Line 192:

=== 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. 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:

+

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 fastest device. Priorities can be assigned in [[fstab]] via the {{ic|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 {{ic|-p}} (or {{ic|--priority}}) parameter of swapon:

+

Or via the {{ic|--priority}} parameter of ''swapon'':

−

# swapon -p 100 /dev/sda1

+

# swapon --priority 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.

=== Using zswap or zram ===

=== Using zswap or zram ===

+

[[Zswap]] is a Linux kernel feature providing a compressed write-back cache for swapped pages. This increases the performance and decreases the IO-Operations. [[ZRAM]] creates a virtual compressed Swap-file in memory as alternative to a swapfile on disc.

[[Zswap]] is a Linux kernel feature providing a compressed write-back cache for swapped pages. This increases the performance and decreases the IO-Operations. [[ZRAM]] creates a virtual compressed Swap-file in memory as alternative to a swapfile on disc.

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 the util-linux package.

Swap space

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), a.k.a "enable swap", and also for suspend-to-disk support.

If it is beneficial to enable swap depends on the amount of installed physical memory, and the amount of memory required to run all the desired programs. If the amount of physical memory is less than the required amount, then it is beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference as swap space. For example, if your programs require 7.5 GB of memory to run, and there are 4 GB of physical memory installed, add the difference of 3.5 GB in swap space. Add more swap space to account for future requirements.
It is a matter of personal preference if you prefer programs to be killed over enabling swap. The biggest drawback to enabling swap is its lower performance, see section #Performance.

To check swap status, use:

$ swapon --show

Or

$ free -h

free also indicates if memory is running short, which can be remedied by enabling swap or increasing swap.

Note: There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way.

Swap partition

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:

Tip: UUIDs and LABELs should be favoured over the use of the device names given by the kernel as the device order could change in the future. See: fstab.

Note:

The fstab-entry is optional if the swap partition is located on a device using GPT. See the next subsection.

If using an SSD with TRIM support, consider using defaults,discard in the swap line in fstab. If activating swap manually with swapon, using the -d/--discard parameter achieves the same. See swapon(8) for details.

Warning: Enabling discard on RAID setups using mdadm will cause system lockup on boot and during runtime, if using swapon.

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 GUID, see systemd#GPT partition automounting for more information.

Disabling swap

To deactivate specific swap space:

# swapoff /dev/sdxy

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.

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).

Warning:Btrfs does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.

Note: If the journal keeps showing the following warning systemd-swap[..]: WARN: swapFC: ENOSPC and no swap file is being created, you need to set swapfc_force_preallocated=1 in /etc/systemd/swap.conf.

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.

Next open /etc/fstab and add

pri=0

to the mount options of 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 encryption

Performance

Swap operations are usually significantly slower than directly accessing data in RAM. Disabling swap entirely to improve performance can sometimes lead to a degradation, since it decreases the memory available for VFS caches, causing more frequent and costly disk I/O.

Swap values can be adjusted to help performance:

Swappiness

The swappinesssysctl 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. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space. Using a low value on sufficient memory is known to improve responsiveness on many systems.

To check the current swappiness value:

$ cat /sys/fs/cgroup/memory/memory.swappiness

or

$ cat /proc/sys/vm/swappiness

Note: As /proc is a lot less organized and is kept only for compatibility purposes, you are encouraged to use /sys instead.

To temporarily set the swappiness value:

# sysctl vm.swappiness=10

To set the swappiness value permanently, edit a sysctl configuration file

VFS cache pressure

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[2]. For more information, see the Linux kernel documentation.

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 fastest device. Priorities can be assigned in fstab via the pri parameter:

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.

Using zswap or zram

Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. This increases the performance and decreases the IO-Operations. ZRAM creates a virtual compressed Swap-file in memory as alternative to a swapfile on disc.

Striping

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.