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

  • Francis Ndungu

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 systems with ≤ 2GB RAM, set swap to 2× RAM.
  • For systems with > 2GB RAM, set swap to 1–1.5× RAM.

For instance:

  • A system with 1GB RAM benefits from a 2GB swap.
  • A system with 4GB RAM may need 4–6GB swap.

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:

  • Low RAM (≤ 4GB): Use higher swappiness (For instance, 60) to prioritize disk usage.
  • High RAM (≥ 8GB): Lower the swappiness (For instance, 10–20) to favor RAM.

  • Check the current swappiness value.

    CONSOLE
    $ cat /proc/sys/vm/swappiness
    

    Output:

    60
    
  • Temporarily set swappiness to 10.

    CONSOLE
    $ sudo sysctl vm.swappiness=10
    
  • Edit /etc/sysctl.conf for a permanent change.

    CONSOLE
    $ sudo nano /etc/sysctl.conf
    
  • Add this line.

    INI
    vm.swappiness=10
    
  • Apply the settings:

    CONSOLE
    $ sudo sysctl -p
    

General Swappiness Recommendations

Systems with Low RAM (≤ 4GB)

  • Use a swappiness of 60 to offload inactive data to disk.

Systems with High RAM (≥ 8GB)

  • Use a swappiness of 10–20 to minimize disk usage.

SSD vs HDD

  • SSD: Use swappiness 30–40 for faster swap performance.
  • HDD: Use swappiness 10–20 to reduce bottlenecks.

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.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI
  • Node.js