How to add swap memory on AlmaLinux 8.5
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.
Table of Contents
- What is swap space/memory?
- Swap memory calculation formula
- Identifying Current Swap Space
- Creating swap file on AlmaLinux 8.5
- Swappiness configuration
- Removing Swap File on AlmaLinux 8.5
- Conclusion
Swap memory calculation formula
Use the following formula to calculate the required swap memory:
# 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
Examples:
- If you have 2 GB of RAM, you need 4 GB of swap memory
- If you have 3 GB of RAM, you need 3 GB of swap memory
- If you have 64 GB of RAM, you need 32 GB of swap memory
Identifying Current Swap Space
Before adding swap space, check if your system already has swap configured. You can use any of these commands:
cat /proc/swaps
Or:
swapon --show
Or:
free
If no swap is configured, these commands will show empty output or zero swap space.
Creating swap file on AlmaLinux 8.5
Follow these steps to create and enable a swap file:
Step 1: Create swap space directory
sudo mkdir /swap-space
Step 2: Install required utility
Install the util-linux
package which provides the fallocate
command:
sudo dnf install util-linux -y
Step 3: Create the swap file
Create a 2GB swap file (adjust the size according to your needs):
sudo fallocate -l 2G /swap-space/swapfile
Step 4: Set proper permissions
For security reasons, the swap file should only be readable and writable by root:
sudo chmod 600 /swap-space/swapfile
Step 5: Configure the swap file
Set up the file as a swap area:
sudo mkswap /swap-space/swapfile
You should see output similar to:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
Step 6: Activate the swap file
Enable the swap file immediately:
sudo swapon /swap-space/swapfile
Step 7: Verify swap is active
Check that the swap is now active:
sudo swapon --show
Output should show:
NAME TYPE SIZE USED PRIO
/swap-space/swapfile file 2G 0B -2
Step 8: Make swap permanent
To ensure the swap file is activated on system boot, add it to /etc/fstab
:
echo '/swap-space/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
Swappiness configuration
Swappiness is a Linux kernel parameter that controls how aggressively the system uses swap space. The value ranges from 0 to 100:
- 0: Swap is used only when RAM is completely full
- 100: System will use swap space aggressively
Check current swappiness value
cat /proc/sys/vm/swappiness
Default value is usually 60.
Temporarily change swappiness
To change swappiness for the current session:
sudo sysctl vm.swappiness=60
Permanently change swappiness
To make the change permanent, edit /etc/sysctl.conf
:
sudo vi /etc/sysctl.conf
Add or modify this line:
vm.swappiness=60
Apply the changes:
sudo sysctl -p
Recommended swappiness values
- Desktop systems: 60 (default)
- Server systems: 10-30
- Database servers: 10 or lower
Removing Swap File on AlmaLinux 8.5
If you need to remove the swap file:
Step 1: Deactivate the swap file
sudo swapoff /swap-space/swapfile
Step 2: Remove the fstab entry
Edit /etc/fstab
and remove the line:
/swap-space/swapfile swap swap defaults 0 0
Step 3: Delete the swap file
sudo rm /swap-space/swapfile
Step 4: Remove the directory (optional)
sudo rmdir /swap-space
Troubleshooting
Issue: “fallocate: fallocate failed: Operation not supported”
Some file systems don’t support fallocate. Use dd
instead:
sudo dd if=/dev/zero of=/swap-space/swapfile bs=1M count=2048
Issue: “swapon: /swap-space/swapfile: swapon failed: Invalid argument”
This usually means the file permissions are incorrect. Ensure:
sudo chmod 600 /swap-space/swapfile
sudo chown root:root /swap-space/swapfile
Issue: System is still slow despite having swap
Remember that swap is much slower than RAM. If your system frequently uses swap:
- Consider adding more physical RAM
- Reduce the number of running applications
- Optimize your applications’ memory usage
Best Practices
- Monitor swap usage: Regularly check swap usage with
free -h
orvmstat
- Size appropriately: Don’t create swap that’s too large; it wastes disk space
- Use SSD if possible: Swap on SSD is faster than on traditional HDD
- Multiple swap spaces: You can have multiple swap files or partitions with different priorities
- Regular maintenance: Monitor for high swap usage which indicates RAM shortage
Conclusion
Adding swap space to AlmaLinux 8.5 is a straightforward process that can help your system handle memory pressure more gracefully. While swap is not a replacement for adequate RAM, it provides a safety net for occasional memory spikes and can prevent out-of-memory errors.
Remember to:
- Calculate the appropriate swap size for your system
- Monitor swap usage regularly
- Adjust swappiness based on your workload
- Consider adding physical RAM if swap is frequently used
With proper configuration, swap space can significantly improve your AlmaLinux system’s stability and performance under memory-intensive workloads.