How to add swap memory on AlmaLinux 8.5
This tutorial explains how to add swap space on AlmaLinuxx
What is swap space/memory?
When the amount of physical memory (RAM) is full, swap space in Linux is used. Non-active pages in memory are moved to the swap memory if the system requires more memory resources and also the RAM is complete.
While swap memory can help devices with a percentage of RAM, it should not be thought about a substitute for more RAM. Swap memory is located on hard disk drives, which have a slower accessibility time than physical memory.
What is the formula to calculate the required swap memory?
The swap space ought to amount to 2x physical RAM for approximately 2 GB of physical RAM, and after that an extra 1x physical RAM for any kind of amount over 2 GB, yet never ever less than 32 MB.
For systems with actually big amounts of RAM (more than 32 GB), you can likely get away with a smaller swap partition (around 1/2 half the physical RAM).
Formula:
# MEMORY = Amount of RAM in GB.
# SWAP_MEMORY = Amount of Swap Memory in GB.
if MEMORY < 2:
SWAP_MEMORY = MEMORY * 2
elif MEMORY > 32:
SWAP_MEMORY = MEMORY / 2
else:
SWAP_MEMORY = MEMORY
Using this formula:
A system with 2 GB of physical RAM would certainly have 4 GB of swap memory.
A system with 3 GB of physical RAM would certainly have 3 GB of swap memory.
A system with 64 GB of physical RAM would certainly have 32 GB of swap memory.
If you intend to upgrade your RAM at a later time, creating a huge swap space partition can be particularly practical.
Identifying current swap space
Before you start, check if your AlmaLinux installation already has swap enabled by typing: cat /proc/swaps
[root@krython ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 2248700 0 -2
Alternatively, use the swapon --show
command:
[root@krython ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2248700 0 -2
Finally, the free
command may also be used:
[root@krython ~]# free
total used free shared buff/cache available
Mem: 828588 154880 465120 5640 208588 544820
Swap: 2248700 0 2248700
In the first two commands if the result is empty it means that your system does not have swap space enabled.
In the last command if the result of the Swap (the total column) is 0 it means that your system does not have swap space enabled.
Creating the swap file
The user that you are logged in as must be a root user or should have sudo privileges to be able to create and add swap space.
In this tutorial, we will add 2GB of Swap memory to our AlmaLinux System.
Example: 12G if you want a 12GB of Swap space
Let's begin:
- Create a swap-space folder in the main directory:
sudo mkdir /swap-space
- Install the fallocate command, we will use it to create the swap file:
sudo dnf install util-linux -y
- Create the swap file inside the swap-space folder that will be used as a Swap space:
sudo fallocate -l 2G /swap-space/swapfile
- Ensure that only the root user can read and write the swap file by setting the correct permissions:
sudo chmod 600 /swap-space/swapfile
- Configure the file as swap:
sudo mkswap /swap-space/swapfile
- Activate the swap space A temporary activate (lost on reboot):
sudo swapon /swap-space/swapfile
- Finally, modify the
/etc/fstab
to automatically add the new swap at system boot time (We will use nano editor):
sudo nano /etc/fstab
if nano isn't already installed on your system!
sudo dnf install epel-release -y && sudo dnf install nano -y
- After opening the
/etc/fstab
file with nano, paste the following line:
- Verify that the swap is active by using either the
swapon
or thefree
command:
[root@krython ~]# sudo swapon -s
NAME TYPE SIZE USED PRIO
/swap-space/swapfile partition 2.1G 0B -2
[root@krython ~]# free -m
total used free shared buff/cache available
Mem: 809 160 258 5 389 521
Swap: 2195 0 2195
What is swappiness and how do I change it?
The swappiness parameter controls the tendency of the kernel to relocate processes out of physical memory and onto the swap disk. This can lead to slower response times for systems and also applications if processes are too strongly moved out of memory due to the fact that disks are much slower than RAM.
swappiness
can have a value of between0
as well as100
swappiness=0
tells the kernel to avoid swapping processes out of physical memory for as long as feasibleswappiness=100
informs the kernel to boldy swap processes out of physical memory as well as relocate them to swap cache
The default setup in AlmaLinux is swappiness=30
.
Lowering the default value of swappiness will possibly boost general performance for a regular AlmaLinux desktop setup.
A value of swappiness=10
is suggested, yet feel free to experiment.
Keep in mind: AlmaLinux server installations have different efficiency requirements to desktop computer systems, and also the default value of 30
is likely more suitable.
- Check the swappiness value:
cat /proc/sys/vm/swappiness
- Change the swappiness value A temporary change (lost on reboot):
sudo sysctl vm.swappiness=60
- Make the change permanent, edit the configuration file located in
/etc/sysctl.conf
sudo nano /etc/sysctl.conf
- Search for
vm.swappiness
and change its value as desired. Ifvm.swappiness
does not exist, add it to the end of the file like so:
- Finally, reboot your system:
sudo reboot
Empty the swap space
Even if you have lots of RAM and also even if you have a reduced swappiness value, it is possible that your computer swaps. This can injure the multitasking efficiency of your desktop computer system.
- You can use the following script to get the swap manually back into RAM:
sudo nano /usr/local/sbin/s2m.sh
- Copy and paste the script into the file:
- Grant the script executable permissions:
sudo chmod +x /usr/local/sbin/s2m.sh
- Run it
sudo /usr/local/sbin/s2m.sh
Remove a swap file
You will do the opposite steps of the installation process, deactivate then remove the swap file
- Deactivating the swap space:
sudo swapoff -v /swap-space/swapfile
- Remove the swap file entry from the
/etc/fstab
file:
- Finally, Delete the swap folder and the swap file:
sudo rm -rf /swap-space
Conclusion
You have learned how to create a swap file and also activate it on your AlmaLinux system.
You have learned how to change the swappiness value on your AlmaLinux system.
You have learned how to deactivate swap space as well as remove your swap file on your AlmaLinux system.