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 systemRecommended swap spaceRecommended swap space if allowing for hibernation
⩽ 2 GB2 times the amount of RAM3 times the amount of RAM
2 GB – 8 GBEqual to the amount of RAM2 times the amount of RAM
8 GB – 64 GBAt least 4 GB1.5 times the amount of RAM
64 GBAt least 4 GBHibernation not recommended
Recommended system swap space

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 the swap is in use.

You should modify swap space while the system is booted in rescue mode, see Debug boot options in the Performing an advanced RHEL installation. When prompted to mount the file system, select Skip.

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
Identifying Current Swap Space Usage
Identifying Current Swap Space Usage

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.

  1. 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.

Prerequisites to resize a partition using fdisk.

  • 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 withumountcommand.

3. Run the following command in order to manage the disk partition.

Managing Disk Partition using FDisk Command

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.

Identifying the Disk Partition with FDisk Command.

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.

Delete a Disk Partition with FDisk Command

Check the result with typing p letter to print out the available disk partitions.

Identifying the disk partition using fdisk command to manage swap space

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.

Creating a new swap space using fdisk command.

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.

Make a swap space partition

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.

Identifying available disks and partitions 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.

Make a partition swap space using mkswap command

10. The final step, find copy the swap space uui, and update the fstab as the screenshot.

Update fstab in Linux Red Hat Enterprise – Manage Swap Space

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.

Comments

  1. 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.

Leave a Comment