How to Manage Swap Space on RHEL/CentOS 8.x
The Swap space partition or file 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. It is easy to manage swap space in Linux.
It’s an important part of maintaining the performance of an RHEL system involves ensuring that adequate swap space is available comparable to the memory demands placed on the system. The goal of this article is to provide an overview of swap management on RHEL 8.x and CentOS 8.x.
- The 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.
- 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.
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 |
Manage Swap Space in Linux
We describe adding more swap space after completing the OS installation. In the case where you upgrade the physical RAM of your computer, and you want to increase the size of swap space. Increasing the swap space is the best practice if you perform memory-intense operations or run applications that require a large amount of memory.
There are 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.
Extending Swap Space to a non-LVM disk environment
In this procedure, we describe how to extend swap space on a non-VLM disk environment. For the first step, we should be identifying the current swap space usage on the system using the following command.
cat /proc/swaps
We can also use the swapon
command alternatively to display the current amount of swap space in a system.
[shais@enlinux ~]$ swapon NAME TYPE SIZE USED PRIO /dev/sda5 partition 3.7G 0B -2
Using the free
command will display the amount of swap space relative to the overall available physical RAM.
- First, disable swapping for the swap partition with
swapoff
command.
[shais@enlinux ~]$ sudo swapoff -v /dev/sda5 swapoff /dev/sda5
2. Make sure the swap space partition is unmounted using cat /proc/swaps
command or lsblk
command.
- A created partition that you know the name of. To check the name, run
cat /etc/fstab
. The first field is the name of the partition. - The only way to change a partition size using
fdisk
is by deleting and recreating it so ensure that the information on the file system is backed up. - Make sure the partition you are resizing is the last partition on a particular disk.
- For the disks with a GUID Partition Table (GPT), using the parted utility is recommended.
Note: When you execute theswapoff -v
command, it will unmount the swap space partition. So you don’t need to unmount the swap space withumount
command.
3. Run the following command in order to manage the disk partition.
The sda
is the disk I’m using for my Linux operating system. Make sure you selected the right disk.
4. Type p
letter to identifying the available partitions on the sda
disk.
The partition /dev/sda5 is the swap space partition. Let’s delete it.
5. To delete a disk partition with fdsik
command, just type the d
letter then type after the disk number.
Check the result with typing p
letter to print out the available disk partitions.
6. To create a new partition for swap space, type n
letter and follow up with entering the size of the partition. In this example I’ve selected +16G
, means 16G of space.
7. NOTE: It is necessary to specify that the new partition is to be a swap partition. The sub-command t
allows you to specify the type of partition. So enter t, specify the partition number, and when it asks for the hex code partition type, type 82, which is the Linux swap partition type, and press Enter.
8. Write the changes with the w
option when you are sure the partitions are correct and ready for making the changes.
9. Check the result of new created partition with lsblk
command.
Here, you see the new created 16G of partition for swap space.
10. Now, make the partition swap space partition with mkswap
command.
10. The final step, find copy the swap space uui, and update the fstab as the screenshot.
Finally, check the swap space with swapon
command and make sure everything is working properly.
[shais@enlinux ~]$ swapon NAME TYPE SIZE USED PRIO /dev/sda5 partition 16G 0B -2
Read more about Linux command line.
It worked perfectly!.
I forgot it to create a swap partition during the Ubuntu installation, but it generated a swapfile. After completing the setup, I’ve replaced the swapfile with disk partition. Everything was okay until the swapon command. It didn’t work alone in Ubuntu. I just specified the swap path like “sudo swapon /dev/nvme0n1p4”, and it picked up the partition as swap space. Thank you.