How to Add Swap Memory in Rocky Linux 9

Updated on January 17, 2025
How to Add Swap Memory in Rocky Linux 9 header image

Introduction

Add Swap Memory in Rocky Linux 9 to enhance system performance by utilizing dedicated disk storage as virtual memory. It is especially beneficial for managing unexpected memory usage spikes and running memory-intensive applications on systems with limited RAM, as it helps free up the primary memory for critical processes.

Follow this guide to add swap memory on Rocky Linux 9 using a dedicated swap file and Vultr Block Storage to increase the available memory on a server.

Prerequisites

Before you begin:

Check Existing Swap Memory in Rocky Linux 9

Swap memory is pre-configured and available on Vultr Rocky Linux 9 servers by default, but the available memory may not be enough based on your needs. Follow the steps below to check the existing swap memory on your server before adding new memory.

  1. Use the free utility with the h human readable option to view the server memory usage information.

    console
    $ free -h
    

    Your output should be similar to the one below.

                   total        used        free      shared  buff/cache   available
    Mem:           3.6Gi       449Mi       3.0Gi        16Mi       359Mi       3.1Gi
    Swap:          2.8Gi          0B       2.8Gi

    The above output includes the total, used, and free memory including swap memory information on the server. The Swap row includes the memory usage information about the active swap memory.

  2. Use the swapon --show command to view detailed information about the active swap memory.

    console
    $ swapon --show
    

    Your output should be similar to the one below.

    NAME      TYPE SIZE USED PRIO
    /swapfile file 2.8G   0B   -2

    /swapfile is the active swap memory on the server with a total size of 2.8 GB based on the above output.

Create Swap Memory

You can create swap memory using a specific file or a disk storage partition. Follow the sections below to create swap memory using a swap file and a Vultr Block Storage device attached to the server.

Create Swap Memory Using a Swap File

Follow the steps below to create swap memory using a swap file while retaining the default swap memory.

  1. Create a new swapfile.img swap file with a specific size such as 2 GB using the fallocate utility in the root directory.

    console
    $ sudo fallocate -l 2G /swapfile.img
    

    Use the dd utility in case fallocate is not available to create the swap file.

    console
    $ sudo dd if=/dev/zero of=/swapfile.img bs=1M count=2048
    
  2. Change the swap file permissions to 600 to grant only the root user read and write privileges to the file.

    console
    $ sudo chmod 600 /swapfile.img
    
  3. Format the file as swap memory using the mkswap command.

    console
    $ sudo mkswap /swapfile.img
    

    Verify the new swap memory size, label information, and UUID value in your output similar to the one below.

    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=4eb99497-a61f-4ff6-979a-aa46453ca02a

If you're using Ubuntu, follow this guide to add swap memory on Ubuntu and optimize your system's performance.

Create Swap Memory Using Vultr Block Storage

Follow the steps below to create swap memory using a Vultr Block Storage device attached to the server.

  1. List all storage devices available on your server using the lsblk utility and verify your Vultr Object Storage disk name.

    console
    $ lsblk
    

    Output:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    sr0     11:0    1 1024M  0 rom  
    vda    253:0    0   30G  0 disk 
    ├─vda1 253:1    0  260M  0 part /boot/efi
    └─vda2 253:2    0 29.7G  0 part /
    vdb    253:16   0   10G  0 disk

    The vdb Vultr Block Storage volume is 10 GB in size and attached to the server as vdb with no active partitions based on the above output.

  2. Initialize the vdb block storage volume using the GPT partition table.

    console
    $ sudo parted -s /dev/vdb mklabel gpt
    
  3. Create a new partition that fills the entire block storage volume.

    console
    $ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%
    
  4. List all storage devices again and verify that the new block storage partition is available.

    console
    $ lsblk
    

    Output:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    sr0     11:0    1 1024M  0 rom  
    vda    253:0    0   30G  0 disk 
    ├─vda1 253:1    0  260M  0 part /boot/efi
    └─vda2 253:2    0 29.7G  0 part /
    vdb    253:16   0   10G  0 disk 
    └─vdb1 253:17   0   10G  0 part

    The new Vultr Block Storage partition is vdb1 based on the above output.

  5. Format the vdb1 partition as swap using the mkswap command.

    console
    $ sudo mkswap /dev/vdb1
    

    Copy the partition's UUID value in your output similar to the one below.

    Setting up swapspace version 1, size = 40 GiB (42947571712 bytes)
    no label, UUID=f8f14363-b7d9-43a6-a797-b981ee5015ba
  6. View the detailed information about all disk partitions and verify that the vdb1 swap partition is available.

    console
    $ sudo blkid
    

    Output:

    /dev/vda2: UUID="3fd6de38-a182-477b-aa6b-7e1881f8c34b" TYPE="ext4" PARTUUID="848fc436-7280-4877-b160-68c3fc776044"
    /dev/vdb1: UUID="f8f14363-b7d9-43a6-a797-b981ee5015ba" TYPE="swap" PARTLABEL="primary" PARTUUID="42115bb4-17dc-42b4-a075-978facea99b7"
    /dev/vda1: SEC_TYPE="msdos" UUID="438B-EA60" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="7d0fc24e-43d0-4c1b-83c1-7ca3e769b067"

Enable Swap Memory

Follow the steps below to enable the swap memory you created using a file or the Vultr Block Storage partition on your server.

  1. Enable swapfile.img as swap memory using the swapon command.

    console
    $ sudo swapon /swapfile.img
    
  2. View the available swap memory on your server and verify that the swapfile.img file is active.

    console
    $ sudo swapon -s
    

    Your output should be similar to the one below.

    Filename                                Type            Size            Used            Priority
    /swapfile                               file            2969596         0               -2
    /swapfile.img                           file            2097148         0               -3
  3. Enable the Vultr Block Storage swap partition as swap memory. Replace /dev/vdb1 with your actual swap partition.

    console
    $ sudo swapon /dev/vdb1
    
  4. View the available swap memory and verify that the swap partition is active.

    console
    $ sudo swapon -s
    

    Your output should be similar to the one below.

    Filename                                Type            Size            Used            Priority
    /swapfile                               file            2969596         0               -2
    /swapfile.img                           file            2097148         0               -3
    /dev/vdb1                               partition       10483708        0               -4

If you're using FreeBSD 14.0, follow this guide to add swap memory on FreeBSD and enhance your system's memory management.

Configure fstab to Automatically Enable Swap Memory

The File Systems Table (fstab) enables automatic mounting of volumes and disk partitions using information in the /etc/fstab configuration file. Follow the steps below to configure fstab to automatically enable swap memory on your server.

  1. Backup the default /etc/fstab configuration.

    console
    $ sudo cp /etc/fstab /etc/fstab.bak
    
  2. Open the /etc/fstab file using a text editor such as nano.

    console
    $ sudo nano /etc/fstab
    
  3. Add the following directive at the end of the file to automatically enable swap memory using the swap file at boot.

    console
    /swapfile.img swap swap defaults 0 0
    

    The above configuration enables automatic mounting using the /swapfile.img file you created earlier. Within the configuration:

    • /swapfile.img: Specifies the swap file to enable.

    • swap: Enables the swap file system type.

    • swap: Sets the swap mount point.

    • 0: Disables backups on the swap file system.

    • 0: Disables file system checks on the swap memory.

    • Add the Vultr Block Storage swap partition using its UUID value such as ceccf757-a255-4ee2-b4eb-a00d706359b2 you copied earlier to automatically mount the volume at boot.

      console
      UUID=ceccf757-a255-4ee2-b4eb-a00d706359b2       swap    swap    sw      0       0
      
    • Add the following directive to automatically enable swap memory using the block storage partition.

      console
      /dev/vdb1 none swap sw 0 0
      

    Save and close the file.

Configure Swappiness

Swappiness is a kernel parameter that determines how often the system swaps processes from main memory (RAM) to swap memory. Its values range from 0 to 100, with lower values like 0 minimizing swapping and higher values like 100 prioritizing swapping processes from RAM to swap memory.

  • 0: Configures the system to avoid swapping processes out of physical memory.
  • 1–49: Enables swapping but reduces its usage unless RAM is at full capacity.
  • 50: Balances swapping and memory caching on the server.
  • 51–99: Gradually increases the swapping of processes from RAM to swap memory.
  • 100: Maximizes swapping, continuously moving processes from RAM to swap memory.
  1. Check the active swappiness value on your server.

    console
    $ cat /proc/sys/vm/swappiness
    

    Your output should be similar to the one below.

    30
  2. Change the swappiness to a new value such as 10.

    console
    $ sudo sysctl vm.swappiness=10
    

    The above command changes the swappiness value from the default 30 to 10 which configures the system to avoid using swap memory unless necessary.

    Output:

    vm.swappiness=10
  3. Reload the sysctl configuration to apply the configuration changes.

    console
    $ sudo sysctl -p
    
  4. View the active swappiness value again and verify that it's set to 10.

    console
    $ cat /proc/sys/vm/swappiness
    

    Output:

    10

Manage Swap Memory

Swap memory is available and automatically enabled on your server at boot based on the configurations you applied earlier. Follow the steps below to manage the swap memory on your server and switch processes with the main memory (RAM).

  1. View all available swap volumes on the server.

    console
    $ swapon -s
    

    Output:

    Filename                                Type            Size            Used            Priority
    /swapfile                               file            2969596         0               -2
    /swapfile.img                           file            2097148         0               -3
    /dev/vdb1                               partition       10483708        0               -4
  2. Enable all inactive swap volumes listed in your /etc/fstab configuration.

    console
    $ sudo swapon -a
    
  3. View the memory usage on your server and verify the swap memory usage.

    console
    $ free -h
    

    Output:

                   total        used        free      shared  buff/cache   available
    Mem:           3.6Gi       457Mi       3.0Gi        16Mi       362Mi       3.1Gi
    Swap:           14Gi          0B        14Gi
  4. Reboot your server to verify that swap memory is automatically enabled at boot.

    console
    $ sudo reboot
    
  5. Run the following swapon --show command and verify that the swap memory is enabled.

    console
    $ swapon -s
    

    Output:

    Filename                                Type            Size            Used            Priority
    /swapfile                               file            2969596         0               -2
    /swapfile.img                           file            2097148         0               -3
    /dev/vdb1                               partition       10483708        0               -4

Remove Swap Memory

Follow the steps below to remove swap memory from your server.

  1. Use the swapoff command to disable the default swap file.

    console
    $ sudo swapoff /swapfile
    
    • For the Vultr Block Storage swap partition:

      console
      $ sudo swapoff /dev/vdb1
      
  2. Delete the default swap file to use the modified /swapfile.img swap memory.

    console
    $ sudo rm /swapfile
    
  3. View the active swap memory on your server and verify that the default swap file is disabled.

    console
    $ swapon -s
    

    Output:

    Filename                                Type            Size            Used            Priority
    /swapfile.img                           file            2097148         0               -2
  4. Open the /etc/fstab file and comment out the automatic mounting directive for the target swap memory.

    console
    $ sudo nano /etc/fstab
    

Conclusion

You have added swap memory on a Rocky Linux 9 server and extended the default system swap memory. Swap memory is slower in performance as compared to RAM but improves the server response rate when running memory-intensive applications. Ensure to monitor the swap memory and increase its size to match your needs depending on the number of active processes. For more swap memory options, run the man swapon command to view the swap manual on your server.