Red Hat System Administration II
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.
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
| RAM | Swap space | Swap space if allowing for hibernation |
|---|---|---|
| 2 GB or less | Twice the RAM | Three times the RAM |
| Between 2 GB and 8 GB | Same as RAM | Twice the RAM |
| Between 8 GB and 64 GB | At least 4 GB | 1.5 times the RAM |
| More than 64 GB | At least 4 GB | Hibernation 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.
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.
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/GNU Parted 3.4 Using /dev/vdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)vdbmkpartPartition name? []?swap1File system type? [ext2]?linux-swapStart?1001MBEnd?1257MB(parted)256MB linux-swap(v1)swap1 (parted)quitInformation: 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 settleThe 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-c5c854605881You 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 ~]#freetotal used free shared buff/cache available Mem: 1873036 134688 1536436 16748 201912 1576044Swap: 00 0 [root@host ~]#swapon /dev/[root@host ~]#vdb2freetotal used free shared buff/cache available Mem: 1873036 135044 1536040 16748 201952 1575680Swap: 2498520 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.
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-reloadBy default, the system uses swap spaces in priority order. The kernel uses the highest priority swap space until it is full, and then starts to use the low priority swap space.
The default priority for swap spaces is low, and new swap spaces are lower priority than older swap spaces. When swap spaces have the same priority, the kernel writes to them in a round-robin fashion.
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.
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?