How to Expand your System RAM with Swap Space on Ubuntu 24.04

  • Francis Ndungu

Introduction

Swap space is a feature in Linux that serves as additional virtual memory by using disk storage. When your system's physical RAM is fully utilized, swap space acts as overflow memory to temporarily store less-used data. This helps prevent memory-related crashes and ensures smooth system performance during heavy workloads.

This guide shows you how to create a swap space on Ubuntu 24.04 to expand your system RAM so that you can run memory-intensive applications efficiently.

Prerequisites

Before you begin:

Check for Existing Swap Space

  1. SSH to your server as a non-root sudo user.
  2. Check if a swap space is already active on your system.

    CONSOLE
    $ sudo swapon --show
    

    If the above command returns no output, it means no swap space is currently active. If there's output, it indicates that swap is already enabled. For example:

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

    If swap is already configured, you don't need to create another one unless you want to replace the existing swap. If you'd rather create a fresh swap file:

    1. Disable the current one first by running the following command.

      CONSOLE
      $ sudo swapoff -a
      
    2. Delete the swap file.

      CONSOLE
      $ sudo rm /swapfile
      

Allocate Disk Space for Swap

Decide the size of the swap space you want to create based on your system's memory requirements and workload. A general rule of thumb is to allocate:

  • Swap space that is 2 times the size of your physical RAM for systems with up to 2GB of physical RAM.
  • Swap space that is 1 to 1.5 times the size of your physical RAM for systems with more than 2GB of physical RAM.

For example:

  • if you only have 1GB of RAM, a swap space of 2GB would help improve performance.
  • If your system has 4GB of physical RAM, a swap space of 4GB to 6GB would be ideal.

Follow the steps below to allocate the collect disk space for your swap.

  1. Create the required swap space. Replace 2G with your required size

    CONSOLE
    $ sudo fallocate -l 2G /swapfile
    
  2. Ensure the new file exists.

    CONSOLE
    $ ls -lh /swapfile
    

    Output

    -rw------- 1 root root ... /swapfile
    

Secure the Swap File

To protect sensitive data in the swap file, restrict access to the root user by setting proper permissions.

  1. Use the Linux chmod command.

    CONSOLE
    $ sudo chmod 600 /swapfile
    
  2. Ensure you have set the permissions correctly.

    CONSOLE
    $ ls -lh /swapfile
    

    Output:

    -rw------- 1 root root ... /swapfile
    

Set up the Swap File

The next step is formatting the file you created above to a valid swap space.

  1. Run the mkswap command.

    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

To ensure your system enables the swap automatically at boot, you must modify the /etc/fstab file and include the details of the new swap file.

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

    CONSOLE
    $ sudo nano /etc/fstab
    
  2. Add the following line at the end of the file and remove any existing /swapfile entry such as /swapfile swap swap defaults 0 0`

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

Optimize Swap Usage

The last setting you should make is adjusting the system's swappiness. Swappiness is a kernel parameter that controls how aggressively the system uses the swap space. It ranges from 0 (minimal swap usage) to 100 (maximum swap usage). The default value is typically 60.

  1. Check the current swappiness value.

    CONSOLE
    $ cat /proc/sys/vm/swappiness
    

    Output:

    60
    
  2. Set the swappiness value to 10 temporarily.

    CONSOLE
    $ sudo sysctl vm.swappiness=10
    

    For a permanent change:

    • Edit the /etc/sysctl.conf file.

      CONSOLE
      $ sudo nano /etc/sysctl.conf
      
    • Add the following line at the end of the file.

      INI
      vm.swappiness=10
      
    • Apply the new settings by rebooting your system or by running the following command.

      CONSOLE
      $ sudo sysctl -p
      
    • Save and close the file.

    The above settings reduces swap usage, ensuring the system prefers RAM over swap for performance.

General Recommendations for Swappiness

1. Systems with Low RAM (≤ 4GB)

  • Use a higher swappiness value, such as 60 (default), to ensure the system swaps less-used data to disk and keeps RAM available for active processes.

2. Systems with High RAM (≥ 8GB)

  • Use a lower swappiness value, such as 10 or 20, to prioritize RAM usage and minimize reliance on slower swap space.

3. SSD vs HDD

  • If your swap is on an SSD, you can afford a slightly higher swappiness, such as 30–40 since SSDs are faster than HDDs.
  • For HDDs, stick to lower values between 10-20 to reduce performance bottlenecks.

Conclusion

This guide explained how to create and configure a swap space on Ubuntu 24.04, including verifying existing swap, allocating disk space, securing and formatting the swap file, making it permanent, and setting the swapiness value. By properly optimizing and managing swap, you can ensure your system handles memory-intensive workloads efficiently, enhancing overall stability and performance.

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