Este conteúdo não está disponível no idioma selecionado.
Chapter 13. Getting started with swap
Swap space provides temporary storage for inactive processes and data when physical memory is full, helping prevent out‑of‑memory errors. It acts as extra memory, allowing the system to keep running. However, swap can slow performance, so optimize physical memory use first.
13.1. Overview of swap space Copiar o linkLink copiado para a área de transferência!
Swap space in Linux is used when physical memory (RAM) is exhausted. When more memory is required and RAM is full, inactive memory pages are moved to swap. Swap helps systems with limited RAM but is no substitute for adding more actual memory.
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.
13.2. Recommended system swap space Copiar o linkLink copiado para a área de transferência!
The recommended size of a swap partition depends 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 set automatically during installation. To allow for hibernation, however, you need to edit the swap space in the custom partitioning stage.
The following recommendations are especially important on systems with low memory, such as 1 GB or less. Failure to allocate sufficient swap space on these systems can 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 |
For border values such as 2 GB, 8 GB, or 64 GB of system RAM, choose swap size based on your needs or preference. If your system resources allow for it, increasing the swap space can 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 free and cat /proc/swaps commands to verify how much and where swap is in use.
Resizing swap space requires temporarily removing it from the system. This can be problematic if running applications rely on the additional swap space and might run into low-memory situations. Preferably, perform swap resizing from rescue mode, see Debug boot options. When prompted to mount the file system, select .
13.3. Creating an LVM2 logical volume for swap Copiar o linkLink copiado para a área de transferência!
You can create an LVM2 logical volume for swap. Assuming /dev/VolGroup00/LogVol02 is the swap volume you want to add.
Prerequisites
- You have enough disk space.
Procedure
Create the LVM2 logical volume of size 4 GB:
lvcreate -n LogVol02 -L 4G VolGroup00
# lvcreate -n LogVol02 -L 4G VolGroup00Copy to Clipboard Copied! Toggle word wrap Toggle overflow Format the new swap space:
mkswap /dev/VolGroup00/LogVol02
# mkswap /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following entry to the
/etc/fstabfile:/dev/VolGroup00/LogVol02 none swap defaults 0 0
/dev/VolGroup00/LogVol02 none swap defaults 0 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow Regenerate mount units so that your system registers the new configuration:
systemctl daemon-reload
# systemctl daemon-reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Activate swap on the logical volume:
swapon -v /dev/VolGroup00/LogVol02
# swapon -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To test if the swap logical volume was successfully created and activated, inspect active swap space by using the following command:
cat /proc/swaps Filename Type Size Used Priority /dev/dm-6 partition 4194300 0 -2
# cat /proc/swaps Filename Type Size Used Priority /dev/dm-6 partition 4194300 0 -2Copy to Clipboard Copied! Toggle word wrap Toggle overflow free -h
# free -h total used free shared buff/cache available Mem: 30Gi 1.2Gi 28Gi 12Mi 995Mi 28Gi Swap: 4.0Gi 0B 4.0GiCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.4. Creating a swap file Copiar o linkLink copiado para a área de transferência!
Set up a swap file to provide virtual memory that prevents system crashes when physical RAM becomes full. This solution is particularly useful when you need additional swap space without repartitioning disks or modifying existing storage configurations.
Prerequisites
- You have enough disk space.
Procedure
- Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the number of 1024-byte sized blocks required for a 64 MB file is 65536.
Create an empty file:
dd if=/dev/zero of=/swapfile bs=1024 count=65536
# dd if=/dev/zero of=/swapfile bs=1024 count=65536Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace 65536 with the value equal to the required block size.
Set up the swap file with the command:
mkswap /swapfile
# mkswap /swapfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the security of the swap file so it is not world readable.
chmod 0600 /swapfile
# chmod 0600 /swapfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/fstabfile with the following entries to enable the swap file at boot time:/swapfile none swap defaults 0 0
/swapfile none swap defaults 0 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow The next time the system boots, it activates the new swap file.
Regenerate mount units so that your system registers the new
/etc/fstabconfiguration:systemctl daemon-reload
# systemctl daemon-reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Activate the swap file immediately:
swapon /swapfile
# swapon /swapfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To test if the new swap file was successfully created and activated, inspect active swap space by using the following command:
cat /proc/swaps
$ cat /proc/swapsCopy to Clipboard Copied! Toggle word wrap Toggle overflow free -h
$ free -hCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.5. Creating a swap volume by using the storage RHEL system role Copiar o linkLink copiado para a área de transferência!
You can use the storage RHEL system role to create or modify swap volumes on block devices.
Prerequisites
- You have prepared the control node and the managed nodes.
- You are logged in to the control node as a user who can run playbooks on the managed nodes.
-
The account you use to connect to the managed nodes has
sudopermissions on them.
Procedure
Create a playbook file, for example,
~/playbook.yml, with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The volume name (
swap_fsin the example) is currently arbitrary. Thestoragerole identifies the volume by the disk device listed under thedisks:attribute.For details about all variables used in the playbook, see the
/usr/share/ansible/roles/rhel-system-roles.storage/README.mdfile on the control node.Validate the playbook syntax:
ansible-playbook --syntax-check ~/playbook.yml
$ ansible-playbook --syntax-check ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.6. Extending swap on an LVM2 logical volume Copiar o linkLink copiado para a área de transferência!
You can extend swap space on an existing LVM2 logical volume. Assuming /dev/VolGroup00/LogVol02 is the volume you want to extend by 2 GB.
Prerequisites
- You have enough disk space.
Procedure
Disable swapping for the associated logical volume:
swapoff -v /dev/VolGroup00/LogVol02
# swapoff -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Resize the LVM2 logical volume by 2 GB:
lvresize -L +2G /dev/VolGroup00/LogVol02
# lvresize -L +2G /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Format the new swap space:
mkswap /dev/VolGroup00/LogVol02
# mkswap /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the extended logical volume:
swapon -v /dev/VolGroup00/LogVol02
# swapon -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To test if the swap logical volume was successfully extended and activated, inspect active swap space:
cat /proc/swaps Filename Type Size Used Priority /dev/dm-6 partition 6291452 0 -2
# cat /proc/swaps Filename Type Size Used Priority /dev/dm-6 partition 6291452 0 -2Copy to Clipboard Copied! Toggle word wrap Toggle overflow free -h
# free -h total used free shared buff/cache available Mem: 30Gi 1.2Gi 28Gi 12Mi 994Mi 28Gi Swap: 6.0Gi 0B 6.0GiCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.7. Reducing swap on an LVM2 logical volume Copiar o linkLink copiado para a área de transferência!
You can reduce swap on an LVM2 logical volume. Assuming /dev/VolGroup00/LogVol02 is the volume you want to reduce.
Procedure
Disable swapping for the associated logical volume:
swapoff -v /dev/VolGroup00/LogVol02
# swapoff -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Clean the swap signature:
wipefs -a /dev/VolGroup00/LogVol02
# wipefs -a /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reduce the LVM2 logical volume by 512 MB:
lvreduce -L -512M /dev/VolGroup00/LogVol02
# lvreduce -L -512M /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Format the new swap space:
mkswap /dev/VolGroup00/LogVol02
# mkswap /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Activate swap on the logical volume:
swapon -v /dev/VolGroup00/LogVol02
# swapon -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To test if the swap logical volume was successfully reduced, inspect active swap space by using the following command:
cat /proc/swaps
$ cat /proc/swapsCopy to Clipboard Copied! Toggle word wrap Toggle overflow free -h
$ free -hCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.8. Removing an LVM2 logical volume for swap Copiar o linkLink copiado para a área de transferência!
You can remove an LVM2 logical volume for swap. Assuming /dev/VolGroup00/LogVol02 is the swap volume you want to remove.
Procedure
Disable swapping for the associated logical volume:
swapoff -v /dev/VolGroup00/LogVol02
# swapoff -v /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Remove the LVM2 logical volume:
lvremove /dev/VolGroup00/LogVol02
# lvremove /dev/VolGroup00/LogVol02Copy to Clipboard Copied! Toggle word wrap Toggle overflow Remove the following associated entry from the
/etc/fstabfile:/dev/VolGroup00/LogVol02 none swap defaults 0 0
/dev/VolGroup00/LogVol02 none swap defaults 0 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow Regenerate mount units to register the new configuration:
systemctl daemon-reload
# systemctl daemon-reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Test if the logical volume was successfully removed, inspect active swap space by using the following command:
cat /proc/swaps
$ cat /proc/swapsCopy to Clipboard Copied! Toggle word wrap Toggle overflow free -h
$ free -hCopy to Clipboard Copied! Toggle word wrap Toggle overflow
13.9. Removing a swap file Copiar o linkLink copiado para a área de transferência!
You can remove a swap file.
Procedure
Disable the
/swapfileswap file:swapoff -v /swapfile
# swapoff -v /swapfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Remove its entry from the
/etc/fstabfile accordingly. Regenerate mount units so that your system registers the new configuration:
systemctl daemon-reload
# systemctl daemon-reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Remove the actual file:
rm /swapfile
# rm /swapfileCopy to Clipboard Copied! Toggle word wrap Toggle overflow