Chapter 15. Swap Space
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. Note that Btrfs does not support swap space.
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. However, modern systems often include hundreds of gigabytes of RAM. As a consequence, recommended swap space is considered a function of system memory workload, not system memory.
Table 15.1, “Recommended System Swap Space” illustrates the recommended size of a swap partition depending on the amount of RAM in your system and whether you want sufficient memory for your system to hibernate. The recommended swap partition size is established automatically during installation. To allow for hibernation, however, you need to edit the swap space in the custom partitioning stage.
Recommendations in Table 15.1, “Recommended System Swap Space” are especially important on systems with low memory (1 GB and less). Failure to allocate sufficient swap space on these systems can cause issues such as instability or even render the installed system unbootable.
Table 15.1. Recommended System Swap Space
|Amount of RAM in the system||Recommended swap space||Recommended swap space if allowing for hibernation|
|⩽ 2 GB||2 times the amount of RAM||3 times the amount of RAM|
|> 2 GB – 8 GB||Equal to the amount of RAM||2 times the amount of RAM|
|> 8 GB – 64 GB||At least 4 GB||1.5 times the amount of RAM|
|> 64 GB||At least 4 GB||Hibernation not recommended|
At the border between each range listed in Table 15.1, “Recommended System Swap Space”, for example a system with 2 GB, 8 GB, or 64 GB of system RAM, discretion can be exercised with regard to chosen swap space and hibernation support. If your system resources allow for it, increasing the swap space may lead to better performance.
Note that distributing swap space over multiple storage devices also improves swap space performance, particularly on systems with fast drives, controllers, and interfaces.
File systems and LVM2 volumes assigned as swap space should not be in use when being modified. Any attempts to modify swap fail if a system process or the kernel is using swap space. Use the
cat /proc/swapscommands to verify how much and where swap is in use.
You should modify swap space while the system is booted in
rescuemode, see Booting Your Computer in Rescue Mode in the Red Hat Enterprise Linux 7 Installation Guide. When prompted to mount the file system, select Skip.
15.1. Adding Swap Space
Sometimes it is necessary to add more swap space after installation. For example, you may upgrade the amount of RAM in your system from 1 GB to 2 GB, but there is only 2 GB of swap space. It might be advantageous to increase the amount of swap space to 4 GB if you perform memory-intense operations or run applications that require a large amount of memory.
You have three options: create a new swap partition, create a new swap file, or extend swap on an existing LVM2 logical volume. It is recommended that you extend an existing logical volume.
15.1.1. Extending Swap on an LVM2 Logical Volume
By default, Red Hat Enterprise Linux 7 uses all available space during installation. If this is the case with your system, then you must first add a new physical volume to the volume group used by the swap space.
After adding additional storage to the swap space's volume group, it is now possible to extend it. To do so, perform the following procedure (assuming
/dev/VolGroup00/LogVol01is the volume you want to extend by 2 GB):
Procedure 15.1. Extending Swap on an LVM2 Logical Volume
- Disable swapping for the associated logical volume:
swapoff -v /dev/VolGroup00/LogVol01
- Resize the LVM2 logical volume by 2 GB:
lvresize /dev/VolGroup00/LogVol01 -L +2G
- Format the new swap space:
- Enable the extended logical volume:
swapon -v /dev/VolGroup00/LogVol01
- To test if the swap logical volume was successfully extended and activated, inspect active swap space:
15.1.2. Creating an LVM2 Logical Volume for Swap
To add a swap volume group 2 GB in size, assuming
/dev/VolGroup00/LogVol02is the swap volume you want to add:
- Create the LVM2 logical volume of size 2 GB:
lvcreate VolGroup00 -n LogVol02 -L 2G
- Format the new swap space:
- Add the following entry to the
/dev/VolGroup00/LogVol02 swap swap defaults 0 0
- Regenerate mount units so that your system registers the new configuration:
- Activate swap on the logical volume:
swapon -v /dev/VolGroup00/LogVol02
- To test if the swap logical volume was successfully created and activated, inspect active swap space:
15.1.3. Creating a Swap File
To add a swap file:
Procedure 15.2. Add a Swap File
- Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
- Create an empty file:
dd if=/dev/zero of=/swapfile bs=1024 count=65536Replace count with the value equal to the desired block size.
- Set up the swap file with the command:
- Change the security of the swap file so it is not world readable.
chmod 0600 /swapfile
- To enable the swap file at boot time, edit
/etc/fstabas root to include the following entry:
/swapfile swap swap defaults 0 0The next time the system boots, it activates the new swap file.
- Regenerate mount units so that your system registers the new
- To activate the swap file immediately:
- To test if the new swap file was successfully created and activated, inspect active swap space: