Swap

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.

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), and also for suspend-to-disk support.

If it is beneficial to extend the virtual memory with swap depends on the amount of installed physical memory. If the amount of physical memory is less than the amount of memory required to run all the desired programs, then it may be 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 (or more) as swap space.

The biggest drawback of enabling swap is its lower performance, see section #Performance. Hence, enabling swap is a matter of personal preference: some prefer programs to be killed over enabling swap and others prefer enabling swap and slower system when the physical memory is exhausted.

Note: There is no performance difference between using a swap partition and a contiguous swap file.

To check swap status, use:

$ swapon --show

Or to show physical memory as well as swap usage:

$ free -h

Swap partition

A swap partition can be created with most GNU/Linux partitioning tools. Swap partitions are designated as type on MBR and on GPT.

To set up a partition as Linux swap area, the command is used. For example:

# mkswap /dev/sdxy

To enable the device for paging:

# swapon /dev/sdxy

To enable this swap partition on boot, add an entry to :

UUID=device_UUID none swap defaults 0 0

where the device_UUID is the UUID of the swap space.

See fstab for the file syntax.

Note: The fstab-entry is optional if the swap partition is located on a device using GPT. See #Activation by systemd.

Activation by systemd

systemd activates swap partitions based on two different mechanisms. Both are executables in . The generators are run on start-up and create native systemd units for mounts. The first, , reads the fstab to generate units, including a unit for swap. The second, 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 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 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).

Swap file creation

Use dd to create a swap file the size of your choosing. For example, creating a 512 MiB swap file:

# dd if=/dev/zero of=/swapfile bs=1M count=512 status=progress

Set the right permissions (a world-readable swap file is a huge local vulnerability):

# chmod 0600 /swapfile

After creating the correctly sized file, format it to swap:

# mkswap -U clear /swapfile

Activate the swap file:

# swapon /swapfile

Finally, edit the fstab configuration to add an entry for the swap file:

For additional information, see fstab#Usage.

Note:
  • The swap file must be specified by its location on the file system, not by its UUID or LABEL.
  • When using Btrfs, do not forget to add the created subvolume to the list as well, and remove the discard,autodefrag options from whichever subvolume gets mounted first (which will control these settings for the whole FS).
  • Compression needs to be disabled as well but the No_COW attribute (previously set with chattr +C) takes care of that.

Remove swap file

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

# swapoff /swapfile
# rm -f /swapfile

Finally, remove the relevant entry from .

systemd-swap

Note: The author now recommends using zram-generator instead, due to low commit frequency and ZRAM covering the needs of most users.

systemd-swap is a script for creating hybrid swap space from ZRAM swaps, swap files and swap partitions. It is not affiliated with the systemd project.

Install the package. Uncomment and set in the Swap File Chunked section of . Start/enable the service.

Visit the author's GitHub page for more information and setting up the recommended configuration.

Swap encryption

See dm-crypt/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 virtual file system (VFS) caches, causing more frequent and costly disk usage.

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 200 (max 100 if Linux < 5.8), the default value is 60. A low value causes the kernel to avoid swapping, a high value causes the kernel to try to use swap space, and a value of 100 means IO cost is assumed to be equal. Using a low value on sufficient memory is known to improve responsiveness on many systems.

To check the current swappiness value:

$ sysctl vm.swappiness

Alternatively, the files (cgroup v1-specific) or can be read in order to obtain the raw integer value.

To temporarily set the swappiness value:

# sysctl -w vm.swappiness=10

To set the swappiness value permanently, create a configuration file. For example:

/etc/sysctl.d/99-swappiness.conf
vm.swappiness = 10

To have the boot loader set swappiness when loading the kernel, add a kernel parameter, e.g. .

To test and more on why this may work, take a look at this article.

VFS cache pressure

Another sysctl parameter that affects swap performance is , 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.

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 (), assign a higher priority to the swap area located on the fastest device. Priorities can be assigned in fstab via the parameter:

/dev/sda1 none swap defaults,pri=100 0 0
/dev/sdb2 none swap defaults,pri=10  0 0

Or via the parameter of swapon:

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

Using zswap or zram

Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages, ZRAM creates a virtual compressed swap block in memory as alternative to a swap partition/file on disk. Both approaches increase the swapping performance and decrease the disk I/O operations.

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 file. Refer to The Software-RAID HOWTO for details.

gollark: I mostly learned to program by doing random projects, reading various random tutorials for things, having to debug weirdly broken stuff, reading some people's code, sort of thing.
gollark: Besides, it's easy to navigate them just by scrolling right/left and having them be divided into multiple windows.
gollark: Also, I get to dazzle people with my tab count.
gollark: Unused tabs don't occupy much RAM, and I enabled the setting to keep tabs when I open and close it, so it works nicely.
gollark: Well, because I inconsistently jump around between various things, and use tabs as markers of "look at this at some point".
This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.