How to Add Swap Memory in Rocky Linux 9
Introduction
Add Swap Memory in Rocky Linux 9 to enhance system performance by utilizing dedicated disk storage as virtual memory. It is especially beneficial for managing unexpected memory usage spikes and running memory-intensive applications on systems with limited RAM, as it helps free up the primary memory for critical processes.
Follow this guide to add swap memory on Rocky Linux 9 using a dedicated swap file and Vultr Block Storage to increase the available memory on a server.
Prerequisites
Before you begin:
- Deploy a Rocky Linux 9 instance on Vultr and enable the Limited User Login feature.
- Deploy a Vultr Block Storage volume and attach it to the server.
- Access the instance using SSH.
- Update the instance.
Check Existing Swap Memory in Rocky Linux 9
Swap memory is pre-configured and available on Vultr Rocky Linux 9 servers by default, but the available memory may not be enough based on your needs. Follow the steps below to check the existing swap memory on your server before adding new memory.
Use the
free
utility with theh
human readable option to view the server memory usage information.console$ free -h
Your output should be similar to the one below.
total used free shared buff/cache available Mem: 3.6Gi 449Mi 3.0Gi 16Mi 359Mi 3.1Gi Swap: 2.8Gi 0B 2.8Gi
The above output includes the total, used, and free memory including swap memory information on the server. The
Swap
row includes the memory usage information about the active swap memory.Use the
swapon --show
command to view detailed information about the active swap memory.console$ swapon --show
Your output should be similar to the one below.
NAME TYPE SIZE USED PRIO /swapfile file 2.8G 0B -2
/swapfile
is the active swap memory on the server with a total size of2.8 GB
based on the above output.
Create Swap Memory
You can create swap memory using a specific file or a disk storage partition. Follow the sections below to create swap memory using a swap file and a Vultr Block Storage device attached to the server.
Create Swap Memory Using a Swap File
Follow the steps below to create swap memory using a swap file while retaining the default swap memory.
Create a new
swapfile.img
swap file with a specific size such as2 GB
using thefallocate
utility in the root directory.console$ sudo fallocate -l 2G /swapfile.img
Use the
dd
utility in casefallocate
is not available to create the swap file.console$ sudo dd if=/dev/zero of=/swapfile.img bs=1M count=2048
Change the swap file permissions to
600
to grant only the root user read and write privileges to the file.console$ sudo chmod 600 /swapfile.img
Format the file as swap memory using the
mkswap
command.console$ sudo mkswap /swapfile.img
Verify the new swap memory size, label information, and
UUID
value in your output similar to the one below.Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=4eb99497-a61f-4ff6-979a-aa46453ca02a
If you're using Ubuntu, follow this guide to add swap memory on Ubuntu and optimize your system's performance.
Create Swap Memory Using Vultr Block Storage
Follow the steps below to create swap memory using a Vultr Block Storage device attached to the server.
List all storage devices available on your server using the
lsblk
utility and verify your Vultr Object Storage disk name.console$ lsblk
Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sr0 11:0 1 1024M 0 rom vda 253:0 0 30G 0 disk ├─vda1 253:1 0 260M 0 part /boot/efi └─vda2 253:2 0 29.7G 0 part / vdb 253:16 0 10G 0 disk
The
vdb
Vultr Block Storage volume is10 GB
in size and attached to the server asvdb
with no active partitions based on the above output.Initialize the
vdb
block storage volume using the GPT partition table.console$ sudo parted -s /dev/vdb mklabel gpt
Create a new partition that fills the entire block storage volume.
console$ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%
List all storage devices again and verify that the new block storage partition is available.
console$ lsblk
Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sr0 11:0 1 1024M 0 rom vda 253:0 0 30G 0 disk ├─vda1 253:1 0 260M 0 part /boot/efi └─vda2 253:2 0 29.7G 0 part / vdb 253:16 0 10G 0 disk └─vdb1 253:17 0 10G 0 part
The new Vultr Block Storage partition is
vdb1
based on the above output.Format the
vdb1
partition as swap using themkswap
command.console$ sudo mkswap /dev/vdb1
Copy the partition's UUID value in your output similar to the one below.
Setting up swapspace version 1, size = 40 GiB (42947571712 bytes) no label, UUID=f8f14363-b7d9-43a6-a797-b981ee5015ba
View the detailed information about all disk partitions and verify that the
vdb1
swap partition is available.console$ sudo blkid
Output:
/dev/vda2: UUID="3fd6de38-a182-477b-aa6b-7e1881f8c34b" TYPE="ext4" PARTUUID="848fc436-7280-4877-b160-68c3fc776044" /dev/vdb1: UUID="f8f14363-b7d9-43a6-a797-b981ee5015ba" TYPE="swap" PARTLABEL="primary" PARTUUID="42115bb4-17dc-42b4-a075-978facea99b7" /dev/vda1: SEC_TYPE="msdos" UUID="438B-EA60" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="7d0fc24e-43d0-4c1b-83c1-7ca3e769b067"
Enable Swap Memory
Follow the steps below to enable the swap memory you created using a file or the Vultr Block Storage partition on your server.
Enable
swapfile.img
as swap memory using theswapon
command.console$ sudo swapon /swapfile.img
View the available swap memory on your server and verify that the
swapfile.img
file 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
Enable the Vultr Block Storage swap partition as swap memory. Replace
/dev/vdb1
with your actual swap partition.console$ sudo swapon /dev/vdb1
View the available swap memory 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 2969596 0 -2 /swapfile.img file 2097148 0 -3 /dev/vdb1 partition 10483708 0 -4
If you're using FreeBSD 14.0, follow this guide to add swap memory on FreeBSD and enhance your system's memory management.
Configure fstab to Automatically Enable Swap Memory
The File Systems Table (fstab
) enables automatic mounting of volumes and disk partitions using information in the /etc/fstab
configuration file. Follow the steps below to configure fstab to automatically enable swap memory on your server.
Backup the default
/etc/fstab
configuration.console$ sudo cp /etc/fstab /etc/fstab.bak
Open the
/etc/fstab
file using a text editor such asnano
.console$ sudo nano /etc/fstab
Add the following directive at the end of the file to automatically enable swap memory using the swap file at boot.
console/swapfile.img swap swap defaults 0 0
The above configuration enables automatic mounting using the
/swapfile.img
file you created earlier. Within the configuration:/swapfile.img
: Specifies the swap file to enable.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 the swap memory.Add the Vultr Block Storage swap partition using its
UUID
value such asceccf757-a255-4ee2-b4eb-a00d706359b2
you copied earlier to automatically mount the volume at boot.consoleUUID=ceccf757-a255-4ee2-b4eb-a00d706359b2 swap swap sw 0 0
Add the following directive to automatically enable swap memory using the block storage partition.
console/dev/vdb1 none swap sw 0 0
Save and close the file.
Configure Swappiness
Swappiness is a kernel parameter that determines how often the system swaps processes from main memory (RAM) to swap memory. Its values range from 0 to 100, with lower values like 0
minimizing swapping and higher values like 100
prioritizing swapping processes from RAM to swap memory.
0
: Configures the system to avoid swapping processes out of physical memory.1–49
: Enables swapping but reduces its usage unless RAM is at full capacity.50
: Balances swapping and memory caching on the server.51–99
: Gradually increases the swapping of processes from RAM to swap memory.100
: Maximizes swapping, continuously moving processes from RAM to swap memory.
Check the active swappiness value on your server.
console$ cat /proc/sys/vm/swappiness
Your output should be similar to the one below.
30
Change the swappiness to a new value such as
10
.console$ sudo sysctl vm.swappiness=10
The above command changes the swappiness value from the default
30
to10
which configures the system to avoid using swap memory unless necessary.Output:
vm.swappiness=10
Reload the
sysctl
configuration to apply the configuration changes.console$ sudo sysctl -p
View the active swappiness value again and verify that it's set to
10
.console$ cat /proc/sys/vm/swappiness
Output:
10
Manage Swap Memory
Swap memory is available and automatically enabled on your server at boot based on the configurations you applied earlier. Follow the steps below to manage the swap memory on your server and switch processes with the main memory (RAM).
View all available swap volumes on the server.
console$ swapon -s
Output:
Filename Type Size Used Priority /swapfile file 2969596 0 -2 /swapfile.img file 2097148 0 -3 /dev/vdb1 partition 10483708 0 -4
Enable all inactive swap volumes listed in your
/etc/fstab
configuration.console$ sudo swapon -a
View the memory usage on your server and verify the swap memory usage.
console$ free -h
Output:
total used free shared buff/cache available Mem: 3.6Gi 457Mi 3.0Gi 16Mi 362Mi 3.1Gi Swap: 14Gi 0B 14Gi
Reboot your server to verify that swap memory is automatically enabled at boot.
console$ sudo reboot
Run the following
swapon --show
command and verify that the swap memory is enabled.console$ swapon -s
Output:
Filename Type Size Used Priority /swapfile file 2969596 0 -2 /swapfile.img file 2097148 0 -3 /dev/vdb1 partition 10483708 0 -4
Remove Swap Memory
Follow the steps below to remove swap memory from your server.
Use the
swapoff
command to disable the default swap file.console$ sudo swapoff /swapfile
For the Vultr Block Storage swap partition:
console$ sudo swapoff /dev/vdb1
Delete the default swap file to use the modified
/swapfile.img
swap memory.console$ sudo rm /swapfile
View the active swap memory on your server and verify that the default swap file is disabled.
console$ swapon -s
Output:
Filename Type Size Used Priority /swapfile.img file 2097148 0 -2
Open the
/etc/fstab
file and comment out the automatic mounting directive for the target swap memory.console$ sudo nano /etc/fstab
Conclusion
You have added swap memory on a Rocky Linux 9 server and extended the default system swap memory. Swap memory is slower in performance as compared to RAM but improves the server response rate when running memory-intensive applications. Ensure to monitor the swap memory and increase its size to match your needs depending on the number of active processes. For more swap memory options, run the man swapon
command to view the swap manual on your server.