Bookmark this page

Manage Swap Space

Objectives

Create and manage swap spaces to supplement physical memory.

Swap Space Concepts

A swap space is an area of a disk under the control of the Linux kernel memory management subsystem. The kernel uses swap space to supplement the system RAM by holding inactive pages in memory. A system's virtual memory encompasses the combined system RAM and swap space.

When the memory usage on a system exceeds a defined limit, the kernel searches through RAM to look for idle memory pages that are assigned to processes. The kernel writes the idle pages to the swap area and reassigns the RAM pages to other processes. If a program requires access to a page on disk, then the kernel locates another idle page of memory, writes it to disk, and recalls the needed page from the swap area.

Because swap areas are on disk, swap is slow when compared with RAM. Although swap space augments system RAM, do not consider swap space as a sustainable solution for insufficient RAM for your workload.

Swap Space Calculation

Administrators should size the swap space based on the memory workload on the system. Application vendors sometimes provide recommendations for calculating swap space. The following table provides guidance based on the total physical memory.

Table 7.1. RAM and Swap Space Recommendations

RAMSwap spaceSwap space if allowing for hibernation
2 GB or lessTwice the RAMThree times the RAM
Between 2 GB and 8 GBSame as RAMTwice the RAM
Between 8 GB and 64 GBAt least 4 GB1.5 times the RAM
More than 64 GBAt least 4 GBHibernation is not recommended

The laptop and desktop hibernation function uses the swap space to save the RAM contents before powering off the system. When you turn the system back on, the kernel restores the RAM contents from the swap space and does not need a complete boot. For those systems, the swap space must be greater than the amount of RAM.

The Knowledgebase article in References at the end of this section gives more guidance about sizing the swap space.

Create Swap Space

To create a swap space, perform the following steps:

  • Create a partition with a file-system type of linux-swap.

  • Place a swap signature on the device.

Create a Swap Partition

Use the parted command to create a partition of the appropriate size and set its file-system type to linux-swap. In the past, tools determined from the partition file-system type whether to activate the device; however, that requirement is no longer the case. Even though utilities no longer use the partition file-system type, administrators can determine the partition's purpose from that type.

The following example creates a 256 MB partition.

[root@host ~]# parted /dev/vdb
GNU Parted 3.4
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  1001MB  1000MB               data

(parted) mkpart
Partition name?  []? swap1
File system type?  [ext2]? linux-swap
Start? 1001MB
End? 1257MB
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name   Flags
 1      1049kB  1001MB  1000MB                  data
 2      1001MB  1257MB  256MB   linux-swap(v1)  swap1

(parted) quit
Information: You may need to update /etc/fstab.

[root@host ~]#

After creating the partition, run the udevadm settle command. This command waits for the system to detect the new partition and to create the associated device file in the /dev directory. The command returns only when it is finished.

[root@host ~]# udevadm settle

Format Swap Space

The mkswap command applies a swap signature to the device. Unlike other formatting utilities, the mkswap command writes a single block of data at the beginning of the device, and leaves the rest of the device unformatted so that the kernel can use it for storing memory pages.

[root@host ~]# mkswap /dev/vdb2
Setting up swapspace version 1, size = 244 MiB (255848448 bytes)
no label, UUID=39e2667a-9458-42fe-9665-c5c854605881

Activate Swap Space

You can use the swapon command to activate a formatted swap space.

Use swapon with the device as a parameter, or use swapon -a to activate all the listed swap spaces in the /etc/fstab file. Use the swapon --show and free commands to inspect the available swap spaces.

[root@host ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1873036      134688     1536436       16748      201912     1576044
Swap:             0           0           0
[root@host ~]# swapon /dev/vdb2
[root@host ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1873036      135044     1536040       16748      201952     1575680
Swap:        249852           0      249852

You can deactivate a swap space with the swapoff command. If pages are written to the swap space, then the swapoff command tries to move those pages to other active swap spaces or back into memory. If the swapoff command cannot write data to other places, then it fails with an error, and the swap space stays active.

Activate Swap Space Persistently

Create an entry in the /etc/fstab file to ensure an active swap space at system boot. The following example shows a typical line in the /etc/fstab file based on the previously created swap space.

UUID=39e2667a-9458-42fe-9665-c5c854605881   swap   swap   defaults   0 0

The example uses the UUID as the first field. When you format the device, the mkswap command displays that UUID. If you lost the output of mkswap, then use the lsblk --fs command. As an alternative, you can use the device name in the first field.

The second field is typically reserved for the mount point. However, for swap devices, which are not accessible through the directory structure, this field takes the swap placeholder value. The fstab(5) man page uses a none placeholder value; however, a swap value gives more informative error messages if something goes wrong.

The third field is the file-system type. The file-system type for swap space is swap.

The fourth field is for options. The example uses the defaults option. The defaults option includes the auto mount option, which activates the swap space automatically at system boot.

The final two fields are the dump flag and the fsck order. Swap spaces do not require backing up or file-system checking, and so these fields should be set to zero.

When you add or remove an entry in the /etc/fstab file, run the systemctl daemon-reload command, or reboot the server, for systemd to register the new configuration.

[root@host ~]# systemctl daemon-reload

Set Swap Space Priority

By default, the system uses swap spaces in series, meaning that the kernel uses the first activated swap space until it is full, and then it starts using the second swap space. You can instead define a priority for each swap space to force a particular order.

To set the priority, use the pri option in the /etc/fstab file. The kernel uses the swap space with the highest priority first. The default priority is -2.

The following example shows three defined swap spaces in the /etc/fstab file. The kernel uses the last entry first, because its priority is set to 10. When that space is full, it uses the second entry, because its priority is set to 4. Finally, it uses the first entry, which has a default priority of -2.

UUID=af30cbb0-3866-466a-825a-58889a49ef33   swap   swap   defaults  0 0
UUID=39e2667a-9458-42fe-9665-c5c854605881   swap   swap   pri=4     0 0
UUID=fbd7fa60-b781-44a8-961b-37ac3ef572bf   swap   swap   pri=10    0 0

Use the swapon --show command to display the swap space priorities.

When swap spaces have the same priority, the kernel writes to them in a round-robin fashion.

 

References

mkswap(8), swapon(8), swapoff(8), mount(8), and parted(8) man pages

Knowledgebase: What Is the Recommended Swap Size for Red Hat Platforms?

Revision: rh134-9.0-fa57cbe