Home → Articles → How to Expand your System RAM with Swap Space on Rocky Linux 9

How to Expand your System RAM with Swap Space on Rocky Linux 9

Introduction

Swap space is a Linux feature that acts as additional virtual memory by using disk storage. When your system's physical RAM is fully utilized, swap space temporarily stores less-used data, ensuring smooth system performance and preventing crashes during heavy workloads.

This guide shows you how to create and configure swap space on Rocky Linux 9 to expand your system RAM and efficiently run memory-intensive applications.

Prerequisites

Before you begin:

Check for Existing Swap Space

  1. SSH into your server as a non-root sudo user.
  2. Check whether a swap space is already active.

    console
    $ sudo swapon --show
    

    If there's no output, it means no swap space is currently active. If swap is already enabled, you'll see output like this.

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

    If swap exists but you'd prefer to create a new one:

    • Disable the current swap space:

      console
      $ sudo swapoff -a
      
    • Delete the swap file (if applicable):

      console
      $ sudo rm /swapfile
      

Allocate Disk Space for Swap

Decide how much swap space to allocate based on your system’s RAM and workload:

For instance:

To allocate swap space:

  1. Create a swap file. Replace 2G with your desired size.

    console
    $ sudo fallocate -l 2G /swapfile
    

    If fallocate isn’t available, use:

    console
    $ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
    
  2. . Verify the file exists.

    console
    $ ls -lh /swapfile
    

    Output:

    -rw------- 1 root root 2.0G ... /swapfile
    

Secure the Swap File

Protect sensitive data in the swap file by restricting access to the root user.

  1. Adjust permissions.

    console
    $ sudo chmod 600 /swapfile
    
  2. Verify the permissions.

    console
    $ ls -lh /swapfile
    

    Output:

    -rw------- 1 root root 2.0G ... /swapfile
    

Set Up the Swap File

  1. Format the file as a swap space.

    console
    $ sudo mkswap /swapfile
    
  2. Activate the swap file.

    console
    $ sudo swapon /swapfile
    
  3. Ensure the swap file is active.

    console
    $ sudo swapon --show
    

    Output:

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

Make the Swap File Permanent

The next step is ensuring your swap space is automatically enabled at boot.

  1. Open the /etc/fstab file with a text editor.

    console
    $ sudo nano /etc/fstab
    
  2. Add this line to the end of the file.

    INI
    /swapfile none swap sw 0 0
    
  3. Save and close the file by pressing Ctrl + X, Y , and Enter.

Optimize Swap Usage

Swappiness determines how aggressively Linux uses swap space. The default value is 60, but you can adjust it for your needs:

General Swappiness Recommendations

Systems with Low RAM (≤ 4GB)

Systems with High RAM (≥ 8GB)

SSD vs HDD

Conclusion

This guide explained how to set up and optimize swap space on Rocky Linux 9, including verifying existing swap, creating and securing a swap file, making it permanent, and tuning swappiness for better performance. With properly configured swap, your system can now handle memory-intensive tasks smoothly and efficiently.