+
+
+
babel
+
nim
swift
โˆž
zorin
sinatra
+
json
+
โˆ‰
couchdb
preact
ocaml
+
protobuf
+
+
+
eslint
+
parcel
+
+
+
qdrant
+
^
gentoo
+
+
rb
+
+
+
smtp
ios
linux
+
+
+
+
py
+
saml
+
go
+
0b
mysql
sklearn
c++
vite
+
+
+
+
xcode
+
webpack
+
linux
+
xgboost
hack
git
intellij
pycharm
+
<-
0x
netlify
+
+
+
redis
smtp
|>
swift
dns
+
laravel
+
+
+
+
Back to Blog
Add Swap Memory on AlmaLinux 8.5 ๐Ÿ’พ
almalinux swap memory

Add Swap Memory on AlmaLinux 8.5 ๐Ÿ’พ

Published Jan 10, 2022

Learn how to add swap memory on AlmaLinux 8.5 to improve system performance. This guide covers creating swap files and swap partitions with best practices.

6 min read
0 views
Table of Contents

Add Swap Memory on AlmaLinux 8.5

Swap memory acts as an overflow area for your RAM. When physical memory is full, inactive pages are moved to swap space. Letโ€™s learn how to add swap memory on AlmaLinux 8.5! ๐Ÿš€

What is Swap Memory?

Swap space is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This allows you to increase the amount of information your server can keep in its working memory.

Checking Current Swap Status

First, letโ€™s check if you have any swap configured:

# Check swap status
sudo swapon --show

# Alternative method
free -h

# Detailed swap information
cat /proc/swaps

Method 1: Creating a Swap File

Step 1: Create Swap File

# Create a 2GB swap file
sudo fallocate -l 2G /swapfile

# Alternative method using dd (if fallocate is not available)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Step 2: Set Correct Permissions

# Only root should be able to read/write
sudo chmod 600 /swapfile

# Verify permissions
ls -lh /swapfile

Step 3: Set Up Swap Space

# Mark file as swap space
sudo mkswap /swapfile

# Output should show something like:
# Setting up swapspace version 1, size = 2 GiB

Step 4: Enable Swap

# Enable the swap file
sudo swapon /swapfile

# Verify it's enabled
sudo swapon --show

Step 5: Make Swap Permanent

# Backup fstab
sudo cp /etc/fstab /etc/fstab.bak

# Add swap to fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Method 2: Creating a Swap Partition

Step 1: Identify Available Disk

# List all disks
sudo fdisk -l

# Or use lsblk
lsblk

Step 2: Create Swap Partition

# Open disk with fdisk (replace /dev/sdb with your disk)
sudo fdisk /dev/sdb

# Inside fdisk:
# n - Create new partition
# p - Primary partition
# Select partition number
# Select first sector (default)
# Select last sector (e.g., +2G for 2GB)
# t - Change partition type
# 82 - Linux swap type
# w - Write changes and exit

Step 3: Create Swap Filesystem

# Create swap on the new partition
sudo mkswap /dev/sdb1

Step 4: Enable Swap Partition

# Enable swap
sudo swapon /dev/sdb1

# Add to fstab for persistence
echo '/dev/sdb1 none swap sw 0 0' | sudo tee -a /etc/fstab

Configuring Swap Settings

Swappiness Value

The swappiness parameter controls how often your system swaps data out of RAM to the swap space:

# Check current swappiness value
cat /proc/sys/vm/swappiness

# Temporarily change swappiness (0-100)
sudo sysctl vm.swappiness=10

# Make it permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Cache Pressure

Adjust how the kernel handles inode and dentry information:

# Check current value
cat /proc/sys/vm/vfs_cache_pressure

# Set cache pressure
sudo sysctl vm.vfs_cache_pressure=50

# Make it permanent
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

Monitoring Swap Usage

Real-time Monitoring

# Monitor swap usage
vmstat 1

# Alternative with more details
sar -S 1 10

# Simple view
free -h

Check Which Processes Use Swap

# Script to check swap usage by process
for file in /proc/*/status ; do
    awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file
done | sort -k 2 -n -r | head -20

Managing Multiple Swap Spaces

Set Swap Priority

# Add swap with priority
sudo swapon -p 10 /swapfile

# In fstab
/swapfile none swap sw,pri=10 0 0
/dev/sdb1 none swap sw,pri=5 0 0

Removing Swap

Disable Swap File

# Disable swap
sudo swapoff /swapfile

# Remove from fstab
sudo sed -i '/swapfile/d' /etc/fstab

# Delete swap file
sudo rm /swapfile

Best Practices

Swap Size Recommendations

RAM SizeRecommended Swap
< 2 GB2x RAM
2-8 GBEqual to RAM
8-64 GBAt least 4 GB
> 64 GBAt least 4 GB

Performance Tips

  1. Use SSD: Place swap on SSD for better performance
  2. Multiple Swap: Use multiple swap spaces with different priorities
  3. Monitor Usage: Regular monitoring helps optimize settings
  4. Swappiness: Lower values (10-30) for desktop, higher (60) for servers

Troubleshooting

Swap Not Activating

# Check for errors
sudo journalctl -xe | grep swap

# Verify swap file integrity
sudo swapon -v /swapfile

Performance Issues

# Check I/O wait
iostat -x 1

# Monitor swap in/out
vmstat 1

Security Considerations

  1. Encryption: Consider encrypting swap for sensitive data
  2. Permissions: Always ensure swap file has 600 permissions
  3. Location: Keep swap files in root filesystem

Conclusion

Youโ€™ve successfully learned how to add swap memory on AlmaLinux 8.5! Swap space provides a safety net for your system when RAM is exhausted, though itโ€™s no substitute for adequate physical memory.

Remember to monitor your swap usage and adjust settings based on your systemโ€™s needs. Happy computing! ๐ŸŽ‰