Add Swap Memory on Ubuntu 24.04

In Ubuntu 24.04, swap memory serves as a crucial component for maintaining system stability when physical RAM becomes exhausted. Swap acts as a specific area of disk storage that functions as virtual memory, enabling the operating system to temporarily move less-used processes out of RAM. This process releases physical memory for more demanding tasks and prevents the system from slowing down or freezing under heavy load. By configuring additional swap space, Ubuntu 24.04 can efficiently manage resource-intensive workloads and multitasking, even on servers with limited RAM capacity.

This guide walks you through the steps of adding swap memory on Ubuntu 24.04. You will create swap space by setting up a dedicated file on your server’s storage and optionally using a separate block storage volume as a swap partition to expand available memory.

Prerequisites

Before proceeding, make sure the following requirements are met:

  • A Debian-based or Ubuntu server deployed on any hosting platform or locally.
  • An optional block storage volume created and attached to your server.
  • SSH access to the server using a non-root account with sudo privileges.

View Existing Swap Memory

Ubuntu 24.04 typically includes swap memory by default, although the allocated size may be minimal. Follow the instructions below to check your system’s current swap configuration before adding more.

To display the system’s memory usage, including RAM and swap, run the following command using the free utility:

You will see an output similar to the following, showing your current swap allocation:

total        used        free      shared  buff/cache   available
Mem:           955Mi       317Mi       201Mi       1.2Mi       598Mi       638Mi
Swap:          2.3Gi       268Ki       2.3Gi

According to the above output, the system currently includes a 2GB swap partition.

Create Swap Memory

Swap space refers to a reserved storage area—either on the main server drive or on a separate block storage device—used as virtual memory. The following sections describe two ways to create swap memory: using a Swapfile or an attached block storage volume.

Create Swap Memory Using a Swapfile on Ubuntu 24.04

To create a 2GB Swapfile in the root directory (/), use the fallocate command as shown below:

$ sudo fallocate -l 2G /swapfile.img

Next, restrict file permissions so that only the root user can read and modify the Swapfile:

$ sudo chmod 0600 /swapfile.img

Convert the file to a swap format with the mkswap utility:

$ sudo mkswap /swapfile.img

You’ll see output similar to this, confirming the swap creation:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=2ed3e083-fac2-4571-bbdf-e9967aa1fc03

Create Swap Memory Using a Block Storage Volume

To set up swap space on a dedicated block storage device, first list all attached storage devices with lsblk:

The new disk should appear as /dev/vdb without any partitions:

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

From this output, the attached block storage device vdb has been successfully recognized with a total capacity of 40GB.

Initialize the volume with a GPT partition table:

$ sudo parted -s /dev/vdb mklabel gpt

Create a partition that spans the entire volume:

$ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%

Confirm the new partition by listing devices again:

Expected output:

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

Format the new partition as swap:

Output confirmation:

Setting up swapspace version 1, size = 40 GiB (42947571712 bytes)
no label, UUID=7b3b6fa6-b344-41bd-b25b-f8657caa36b4

Display device information with blkid to confirm the swap partition:

Example output:

/dev/vda2: UUID="95e88749-c308-4c15-aca0-f47049d0c699" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="48874572-7e1f-4766-93e7-431038bd78f3"
/dev/vda1: UUID="D587-7645" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="96ca28e5-4696-482c-9359-24b87f2ea53e"
/dev/vdb1: UUID="ca230c16-b5a4-44e6-b5a8-930bb1f33fcf" TYPE="swap" PARTLABEL="primary" PARTUUID="7897c216-dc99-4e91-be17-abc1a0dba849"

Make note of the UUID for /dev/vdb1; it will be used later for automatic mounting during boot.

Configure Swappiness

The Swappiness setting determines how your system manages the balance between physical memory (RAM) and swap memory based on utilization. A lower value minimizes the use of swap space, while a higher one allows the operating system to offload processes to disk more frequently. Below is an overview of how swappiness values affect performance:

  • 0: Prevents swapping processes out of physical RAM.
  • 1–49: Uses swap minimally until physical memory reaches its limit.
  • 50: Balances memory caching and swap usage evenly.
  • 51–99: Encourages more swapping between memory and disk.
  • 100: Prioritizes swap heavily, constantly moving processes from RAM to swap space.

Follow the steps below to configure your preferred swappiness level on Ubuntu 24.04.

Run the following command to modify the /etc/sysctl.conf file and define your swappiness value. Replace 50 with the value you prefer:

$ echo "vm.swappiness = 50" | sudo tee -a /etc/sysctl.conf

Expected output:

Reload the sysctl configuration to apply the changes:

Test the Swap Memory

To review all active swap areas on your Ubuntu server, execute the following command:

The output will look similar to this:

Filename                                Type            Size            Used            Priority
/swapfile                               file            2457596         268             -2
/swapfile.img                           file            2097148         0               -3
/dev/vdb1                               partition       41940988        0               -4

To activate all swap entries listed in your /etc/fstab configuration, use:

Next, display memory usage to confirm active swap allocation:

Example output:

               total        used        free      shared  buff/cache   available
Mem:           955Mi       348Mi       155Mi       1.2Mi       613Mi       607Mi
Swap:           44Gi       268Ki        44Gi

Remove Swap Memory

If you need to disable swap, use the swapoff command followed by the path to the Swapfile or swap partition. For example, disable the default Swapfile as shown below:

Next, check to see if the swap file has actually been disabled:

Sample output:

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

conclusion

You have successfully set up additional swap space on an Ubuntu 24.04 server, thereby expanding the system’s virtual memory. Although swap is slower than RAM, it improves system performance by moving less frequently used processes out of RAM. If you’d like to learn about additional options and parameters, you can open the swap man page with the following command:

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: