The cart is empty

Adding and expanding the SWAP partition or SWAP file on Linux distributions like CentOS and AlmaLinux is a basic process that can improve system performance, especially if you run memory-intensive applications. This article provides a step-by-step guide on how to effectively add SWAP on these distributions.

What is SWAP and Why is it Important?

SWAP is a virtual memory space on the disk that serves as an extension of physical RAM. When the system runs out of physical memory (RAM), it starts moving inactive data to the SWAP space. Although access to SWAP is slower than to physical memory, it can significantly reduce the risk of memory exhaustion and application crashes.

How to Check Current SWAP Size?

Before adding or expanding SWAP, check the current memory and SWAP status. This command will display memory usage:

free -h

The output will show the total physical memory (RAM) and the currently allocated SWAP. You can also use the following command for more detailed information:

swapon --show

How to Add a SWAP File on CentOS and AlmaLinux

If you don't have a free SWAP partition, you can create a SWAP file, which works just like a SWAP partition. This process involves several steps.

1. Create a New SWAP File

First, use the fallocate or dd command to create the file. Here's an example of creating a 2 GB SWAP file:

sudo fallocate -l 2G /swapfile

If your system doesn't support fallocate, you can use the dd command:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

2. Set Correct Permissions

It's important to set the correct permissions so that only the root user can use the file:

sudo chmod 600 /swapfile

3. Convert the File to SWAP

Now, convert the file into SWAP space:

sudo mkswap /swapfile

4. Activate the SWAP File

Activate the new SWAP file with this command:

sudo swapon /swapfile

You can check if the SWAP was successfully activated using the command:

swapon --show

5. Make the SWAP Permanent

To ensure the SWAP file remains active after a reboot, add it to the /etc/fstab file:

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

How to Remove a SWAP File

If you want to remove the SWAP file, follow these steps:

1. Deactivate SWAP

sudo swapoff /swapfile

2. Remove the Entry from /etc/fstab

Edit the /etc/fstab file and remove the line containing /swapfile.

3. Delete the File

sudo rm /swapfile

Expanding SWAP on an Existing SWAP Partition

If you already have a SWAP partition and want to expand it, the process is somewhat more complex and involves resizing partitions using tools like fdisk or parted. The process includes:

  • Backing up data if you're working on a production server.
  • Turning off the existing SWAP using swapoff.
  • Resizing the partition using fdisk or parted.
  • Updating the /etc/fstab file and activating the new SWAP partition.

 

Adding or expanding SWAP on CentOS and AlmaLinux is a relatively simple process that can significantly improve your system's performance, especially if you have limited physical memory. Be sure to monitor your SWAP and RAM usage to prevent performance issues. If you have enough disk space, it’s always recommended to have a reasonably sized SWAP, especially for systems with low RAM capacity.