Swap Space
7.1. What is 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. But because the amount of memory in modern systems has increased into the hundreds of gigabytes, it is now recognized that the amount of swap space that a system needs is a function of the memory workload running on that system. However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, we recommend determining system swap using the following table.
Table 7.1. Recommended System Swap Space
Amount of RAM in the System | Recommended Amount of Swap Space |
---|---|
4GB of RAM or less | a minimum of 2GB of swap space |
4GB to 16GB of RAM | a minimum of 4GB of swap space |
16GB to 64GB of RAM | a minimum of 8GB of swap space |
64GB to 256GB of RAM | a minimum of 16GB of swap space |
256GB to 512GB of RAM | a minimum of 32GB of swap space |
Important
File systems and LVM2 volumes assigned as swap space
cannot be in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap should be allocated and used by the kernel. Use the
free
and
cat /proc/swaps
commands to verify how much and where swap is in use.
The best way to achieve swap space modifications is to boot your system in rescue mode, and then follow the instructions (for each scenario) in the remainder of this chapter. Refer to the Red Hat Enterprise Linux Installation Guide for instructions on booting into rescue mode. When prompted to mount the file system, select
.
7.2.3. Creating a Swap File
To 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.
-
At a shell prompt as root, 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
-
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. -
After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command
cat /proc/swaps
orfree
.
7.3.3. Removing a Swap File
To remove a swap file:
-
At a shell prompt as root, execute the following command to disable the swap file (where
/swapfile
is the swap file):swapoff -v /swapfile
-
Remove its entry from the
/etc/fstab
file. -
Remove the actual file:
rm /swapfile