How to increase swap size on RHEL 8 / CentOS 8

On a system with memory-intense workload with common peak loads, a large swap memory can be useful to store large memory contents not needed at the moment. While using swap instead of memory will certainly have great impact on performance, sometimes this is preferable over adding more memory to the machine, as disk space is much cheaper. Sometimes there is simply no more memory, maybe a physical machine that is out of free slots, and there isn’t any larger memory modules on the market. At other times the slower performance on peak loads may be preferable over the application crashing with out of memory error.

In some cases swap memory needs to be increased, a live example could be expanding the memory in the machine, and so also expanding the swap space to match the double of the new memory size.

In this tutorial you will learn:

  • How to check swap space.
  • How to identify swap volume.
  • How to extend swap volume.
  • How to add another swap volume.

Checking swap size and usage with the free command

Checking swap size and usage with the free command.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System RHEL 8 / CentOS 8
Software lvm2-2.03.00-0.4, util-linux-2.32.1-6
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to increase swap size on RHEL 8 step by step instructions



In our lab setup, the swap partition is on a logical volume. This is the most common setup, as LVM is a very flexible way to handle disk space. The LVM tutorial covers the architecture and usage of the Logical Volume Manager, so we will not go into details when using lvextend to increase the size of the swap volume. We’ll increase the initial 1GB swap to 2GB.

  1. We need to find out which volume is our swap partition:

    # grep swap /etc/fstab 
    /dev/mapper/rhel_rhel8lab-swap swap                    swap    defaults        0 0
    

    As by default volume names are generated with short hostname, and while creating a logical volume we can explicitly name it, the volume name will vary.

    In our example we take note of the volume name with the path, which is /dev/mapper/rhel_rhel8lab-swap or /dev/rhel_rhel8lab/swap. These two names for the partition may be confusing at first glance, but if we list both, we can see they are actually symlinks pointing to the same device:

    # ls -al /dev/rhel_rhel8lab/swap
    lrwxrwxrwx. 1 root root 7 Nov 25 18:35 /dev/rhel_rhel8lab/swap -> ../dm-1
    # ls -al /dev/mapper/rhel_rhel8lab-swap
    lrwxrwxrwx. 1 root root 7 Nov 25 18:35 /dev/mapper/rhel_rhel8lab-swap -> ../dm-1
    

    We’ll use these interchangeably in the next steps.

  2. We need to disable swapping on the volume while we work on it. We use the full volume name that we found in the fist step.

    # swapoff -v /dev/mapper/rhel_rhel8lab-swap
    swapoff /dev/mapper/rhel_rhel8lab-swap
    
    WARNING
    Do not disable swap on a production server while it is under heavy load, and is using the swap excessively! In such a scenario add another swap partition instead.


  3. To increase the size of the swap we extend the logical volume holding it. In this example with one more Gigabyte:

    # lvextend -L +1G /dev/rhel_rhel8lab/swap 
      Size of logical volume rhel_rhel8lab/swap changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).
      Logical volume rhel_rhel8lab/swap successfully resized.
    

    Note that you need to ensure you have sufficient free space in the volume group holding the logical volume, as you would on any other volume extension that contains filesystems.

  4. We re-create the swap on the extended volume:

    # mkswap /dev/mapper/rhel_rhel8lab-swap 
    mkswap: /dev/mapper/rhel_rhel8lab-swap: warning: wiping old swap signature.
    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=defca15e-a5ed-4fe8-bddd-5f11a3c76e80
    
  5. We turn swapping back on on the extended swap volume:

    # swapon -v /dev/mapper/rhel_rhel8lab-swap 
    swapon: /dev/mapper/rhel_rhel8lab-swap: found signature [pagesize=4096, signature=swap]
    swapon: /dev/mapper/rhel_rhel8lab-swap: pagesize=4096, swapsize=2147483648, devsize=2147483648
    swapon /dev/mapper/rhel_rhel8lab-swap
    
  6. It is a good practice to always verify that the modification of the system is successful. In this case free -m (memory information with values in Megabytes) should show that we have 2GB of swap:

    # free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1989        1201         225          17         562         617
    Swap:          2047           0        2047
    

How to Add new swap partition on RHEL 8 step by step instructions

When disabling the swap to increase it’s size is not appropriate, we can increase overall swap by adding another volume that is a swap partition as well. When we are finished we simply give it to the system, which will start using it as needed.

  1. We create a new logical volume with LVM as we would if we’d like a new volume that stores a filesystem:



    # lvcreate -L 1G -n swap2 rhel_rhel8lab
      Logical volume "swap2" created.
    

    On the same lab machine this will result with a full path name of the new partition as /dev/mapper/rhel_rhel8lab-swap2, the same as /dev/rhel_rhel8lab/swap2.

  2. Create the swap with mkswap:

    # mkswap /dev/rhel_rhel8lab/swap2
    Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
    no label, UUID=a319fb8d-18b8-42b7-b6bf-cafb27aaec2b
    
  3. Turn on swap on the new volume:

    # swapon /dev/rhel_rhel8lab/swap2
  4. And verify the results:

    # free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1989        1198         153          14         637         623
    Swap:          2047           0        2047
    
  5. For the operating system to be able to use the new swap partition after reboot, we need to add it to /etc/fstab:

    # grep swap /etc/fstab 
    /dev/mapper/rhel_rhel8lab-swap swap                    swap    defaults        0 0
    /dev/mapper/rhel_rhel8lab-swap2 swap                    swap    defaults        0 0
    

    The difference between the two methods can be outlined by checking the contents of /proc/swaps, where we can see that by adding another volume instead of extending the one we already have will result in two devices used for swapping:

    # cat /proc/swaps 
    Filename				Type		Size	Used	Priority
    /dev/dm-1                               partition	1048572	0	-2
    /dev/dm-2                               partition	1048572	0	-3