How to Add Swap Memory on Debian 12

Updated on November 21, 2023
How to Add Swap Memory on Debian 12 header image

Introduction

Swap memory, also known as swap space, is a dedicated storage part used as an overflow space for the system's memory. When Random Access Memory (RAM) is fully utilized, the operating system moves inactive pages of memory to the swap space to free up RAM for actively running processes. The swapping process improves system performance and prevents process failures by ensuring that memory is available for active applications.

This article explains how to add swap memory on Debian 12. You will set up swap memory using a dedicated file on your server storage and use Vultr Block Storage as a dedicated swap volume to increase the available server memory.

Prerequisites

Before you begin, you should:

Check the Existing Swap Memory

View the existing swap memory to determine the available capacity to modify or create on your server. Follow the steps below to use the free utility and view the existing swap memory on your server.

  1. View the available server memory including RAM and Swap.

    console
    $ sudo free -h
    

    Verify the total swap memory available on your server similar to the output below.

    total        used        free      shared  buff/cache   available
    Mem:           3.8Gi       381Mi       3.3Gi       744Ki       396Mi       3.4Gi
    Swap:          2.8Gi          0B       2.8Gi

    The above command displays the total, used, and free memory, including swap memory on your server.

  2. Use the swapon command to view a more detailed output of the swap space.

    console
    $ sudo swapon --show
    

    Your output should be similar to the one below.

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

    If no output is available, then your server does not have any active swap memory.

Create Swap Memory

Swap memory is dedicated space on a storage device such as your server storage or a block storage volume. Follow the sections below to create swap memory using either a Swapfile or a dedicated Vultr Block Storage volume attached to your server.

Create Swap Memory Using a Swapfile

  1. Create a Swapfile with a specific size of your choice. For example, run the following command to create a swapfile with 2GB using the fallocate utility in your root directory /.

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

    If fallocate is unavailable, use the dd utility to create the Swapfile.

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

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

    console
    $ sudo mkswap /swapfile.img
    

    Verify the new swap memory size, label information, and the UUID when successful similar to the output below.

    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=c0cdbc5c-e28b-4227-a22e-fbecd4dbf7f8

Create Swap Memory using Vultr Block Storage

Follow the sections below to create swap memory using a Vultr Block Storage volume attached to the server.

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

    console
    $ sudo lsblk
    

    Output:

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

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

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

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

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

    console
    $ sudo lsblk
    

    Output:

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
    sr0     11:0    1 1024M  0 rom  
    vda    254:0    0   30G  0 disk 
    ├─vda1 254:1    0  512M  0 part /boot/efi
    └─vda2 254:2    0 29.5G  0 part /
    vdb    254:16   0   40G  0 disk 
    └─vdb1 254:17   0   40G  0 part
  5. Format the block storage as swap memory. Replace /dev/vdb1 with your actual GPT partition path.

    console
    $ sudo mkswap /dev/vdb1
    

    Output:

    Setting up swapspace version 1, size = 40 GiB (42947571712 bytes)
    no label, UUID=4bf54a86-9516-4987-8a78-195b6c74e241
  6. View your block device information and verify that the new swap partition is available.

    console
    $ sudo blkid
    

    Output:

    /dev/vdb1: UUID="4bf54a86-9516-4987-8a78-195b6c74e241" TYPE="swap" PARTLABEL="primary" PARTUUID="f3f6fbe3-38ef-4983-a2af-feb0cf5a2d18"
    /dev/vda2: UUID="ceccf757-a255-4ee2-b4eb-a00d706359b2" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="220cd443-75de-4b2b-b66e-d9b5b24cb22d"
    /dev/vda1: UUID="FDD2-F69B" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="a7b2e7cf-7e86-4420-a46f-d88c18ab7e38"

    Note the swap partition UUID value to use when mounting the volume automatically at boot time.

Enable Swap Space

Swap space works as dedicated memory when enabled on your server which enables the system to swap processes between the main memory and your swap space. Follow the steps below to enable the swap space you created earlier on your server.

  1. Activate your existing Swapfile as swap memory on your server.

    console
    $ sudo swapon /swapfile.img
    
  2. View the swap memory available on your server and verify that your Swapfile 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 your Vultr Block Storage swap partition as swap memory. Replace /dev/vdb1 with your actual partition path.

    console
    $ sudo swapon /dev/vdb1
    
  4. View the swap memory available on your server 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            2457596         268             -2
    /swapfile.img                           file            2097148         0               -3
    /dev/vdb1                               partition       41940988        0               -4

Configure the File System Table (fstab) to Enable Automatic Mounting

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

  1. Back up the original fstab configuration on your server.

    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 enable automatic mounting of swap memory using your Swapfile.

    console
    /swapfile.img swap swap defaults 0 0
    

    Save and close the file.

    The above configuration enables automatic mounting using the /swapfile.img file on your server. Within the configuration:

    • 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 swap memory.

    • Add the following directive at the end of the file to enable the automatic mounting on the block storage swap partition.

      console
      /dev/vdb1 none swap sw 0 0
      

      Save and close the file.

Configure Swappiness Value

Swappiness is a kernel parameter that determines the balance between using swap space and RAM. Swappiness value range from 0 to 100, where lower values such as 0 reduce swapping and higher values 100 prioritize swapping of processes from RAM to the swap memory.

  • 0: Enables the system to avoid swapping processes out of physical memory.
  • 1-49: Enables swapping with reduced usage unless the main memory (RAM) is at capacity.
  • 50: Balances swapping and memory caching on the server.
  • 51-99: Increases swapping from the main memory to swap memory.
  • 100: Prioritizes swapping on the server and processes are continuously moved from RAM to swap.
  1. View the active swappiness value on your server.

    console
    $ cat /proc/sys/vm/swappiness
    

    Your output should be similar to the one below.

    60
  2. Change the swappiness value to 10. Replace 10 with your desired swapping level.

    console
    $ sudo sysctl vm.swappiness=10
    

    The above changes the active swappiness value to 10 to enable the system avoid using swap space unless necessary.

    Output:

    vm.swappiness=10
  3. Reload the sysctl configuration to apply your 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

Test and Validate Swap Memory

Swap memory is available on your server. Follow the steps below to test and validate the available volumes actively swapping processes with your server's main memory.

  1. View all available swap volumes on your server.

    console
    $ sudo swapon -s
    

    Output:

    Filename                                Type            Size            Used            Priority
    /swapfile                               file            2969596         0               -2
    /swapfile.img                           file            2097148         0               -3
    /dev/vdb1                                partition       41943036        0               -4
  2. Run the following command to activate all inactive swap volumes enabled in your /etc/fstab configuration.

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

    console
    $ free -h
    

    Output:

                   total        used        free      shared  buff/cache   available
    Mem:           3.8Gi       440Mi       1.2Gi       760Ki       2.5Gi       3.4Gi
    Swap:           44Gi          0B        44Gi

Remove Swap Memory

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

  1. Deactivate the default Swapfile using the swapoff command.

    console
    $ sudo swapoff /swapfile
    
  2. Deactivate your block storage swap partition.

    console
    $ sudo swapoff /dev/vdb1
    
  3. Delete the default Swapfile to use your modified swap space.

    console
    $ sudo rm /swapfile
    
  4. View all swap volumes on your server and verify that the default Swapfile is disabled.

    console
    $ sudo swapon -s
    

    Output:

    Filename                                Type            Size            Used            Priority
    /swapfile.img                           file            2097148         0               -2

Conclusion

You have added swap memory on your Debian 12 server and extended the default system memory to enable swapping of processes to improve the general system performance. Swap memory may be slower as compared to RAM but improves the server response rate when running memory-intensive applications. Monitor your swap memory regularly to ensure optimal performance and make adjustments depending on your processing needs. For more configuration options, run the man swapon command to view the swap manual on your server.