Este conteúdo não está disponível no idioma selecionado.
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.
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” provides 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.
Important
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 may cause issues such as instability or even render the installed system unbootable.
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.
Important
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
free
and cat /proc/swaps
commands to verify how much and where swap is in use.
You should modify swap space while the system is booted in
rescue
mode, see Booting Your Computer with the Rescue Mode in the Red Hat Enterprise Linux 6 Installation Guide. When prompted to mount the file system, select .
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 6 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. For instructions on how to do so, refer to Section 14.2.2, “Adding Unallocated Volumes to a Volume Group”.
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/LogVol01
is 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:
# mkswap /dev/VolGroup00/LogVol01
- Enable the extended logical volume:
# swapon -v /dev/VolGroup00/LogVol01
To test if the logical volume was successfully extended, use
cat /proc/swaps
or free
to inspect the swap space.
15.1.2. Creating an LVM2 Logical Volume for Swap
To add a swap volume group (assuming
/dev/VolGroup00/LogVol02
is 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:
#
mkswap /dev/VolGroup00/LogVol02
- Add the following entry to the
/etc/fstab
file:#
/dev/VolGroup00/LogVol02 swap swap defaults 0 0
- Enable the extended logical volume:
#
swapon -v /dev/VolGroup00/LogVol02
To test if the logical volume was successfully created, use
cat /proc/swaps
or free
to inspect the 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.
- Type the following command with
count
being equal to the desired block size:#
dd if=/dev/zero of=/swapfile bs=1024 count=65536
- Setup the swap file with the command:
#
mkswap /swapfile
- It is recommended that the permissions are changed to prevent the swap being world readable.
#
chmod 0600 /swapfile
- To enable the swap file immediately but not automatically at boot time:
#
swapon /swapfile
- To enable it at boot time, edit
/etc/fstab
to include the following entry:/swapfile swap swap defaults 0 0
The next time the system boots, it enables the new swap file.
To test if the new swap file was successfully created, use
cat /proc/swaps
or free
to inspect the swap space.