VPSMATE

  • HOME
  • NEWS
  • VPS
  • DEDICATED
  • ARCHIVES
  • GoDaddy
  • SCRIPT
  • TOOLS
    • Traceroute
    • Benchmark
    • Useful Sites
  • Themes
  • GFW
VPSMATE
VPS COUPON & PROMOS | VPS & DEDICATED SERVERS OFFERS | VPS REVIEWS
  1. Main page
  2. Tips
  3. Main content

Add swap memory on AlmaLinux 8.5

30/08/2022 1063hotness 0likes 0comments

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 determine 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).

 

So, if:

# 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@vpsmate~]# cat /proc/swaps

Filename Type Size Used Priority

/dev/dm-1 partition 2248700 0 -2

 

Alternatively, use the swapon --show command:

 

 

[root@vpsmate~]# swapon -s

Filename Type Size Used Priority

/dev/dm-1 partition 2248700 0 -2

 

 

Finally, the free command may also be used:

 

 

[root@vpsmate~]# 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.

 

NOTE:  A System may have multi swap partitions

 

 

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.

 

 

Note: Replace the 2G with the size of the swap memory you need.

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

 

 

 

Note: if nano isn't already installed on your system, install it

sudo dnf install epel-release -y &&  sudo dnf install nano -y

 

  • After opening the /etc/fstab file with nano,  paste the following line:

 

 

/swap-space/swapfile swap swap defaults 0 0

 

Save & Exit with (ctrl+x) then type: y then hit enter

 

  • Verify that the swap is active by using either the swapon or the free command:

 

 

 

[root@vpsmate~]# sudo swapon -s

NAME TYPE SIZE USED PRIO

/swap-space/swapfile partition 2.1G 0B -2

 

[root@vpsmate~]# 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 between 0 as well as 100
  • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as feasible
  • swappiness=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. If vm.swappiness does not exist, add it to the end of the file like so:

 

 

vm.swappiness=60

 

Save & Exit with (ctrl+x) then type: y then hit enter

 

  • 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:

 

 

 

#!/bin/sh

 

mem=$(LC_ALL=C free | awk '/Mem:/ {print $4}')

swap=$(LC_ALL=C free | awk '/Swap:/ {print $3}')

 

if [ $mem -lt $swap ]; then

echo "ERROR: not enough RAM to write swap back, nothing done" >&2

exit 1

fi

 

swapoff -a &&

swapon -a

 

 

Save & Exit with (ctrl+x) then type: y then hit enter

 

  • 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:

 

 

 

sudo nano /etc/fstab

 

# remove the line --> /swap-space/swapfile swap swap defaults 0 0

 

 

Save & Exit with (ctrl+x) then type: y then hit enter

 

  • Finally, Delete the swap folder and the swap file:

 

 

sudo rm -rf /swap-space

 

 

Conclusion

 

  1. You have learned how to create a swap file and also activate it on your AlmaLinux 8.5 system.
  2. You have learned how to change the swappiness value on your AlmaLinux 8.5 system.
  3. You have learned how to deactivate swap space as well as remove your swap file on your AlmaLinux 8.5 system.

 

Related

Tag: AlmaLinux swap
Last updated:30/08/2022

kyaky

This person is a lazy dog and has left nothing

Like
< Last article
Next article >

Comments

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
Cancel

COPYRIGHT © 2021 vpsmate.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang