The cart is empty

Swap space in Linux-based operating systems, such as CentOS 7, acts as virtual memory, allowing the system to move portions of data from physical memory (RAM) to disk, thus preventing physical memory exhaustion under high loads. Increasing swap space can be beneficial in situations where you need to augment available memory for applications or processes without adding physical RAM. Here's a step-by-step guide on how to achieve this on CentOS 7.

Preparation

Before you begin, it's good to check the current size and usage of swap space using the command:

swapon -s

or

free -m

It's also important to ensure that there is enough free space on disk for increasing swap space.

Step 1: Turning Off Swap Space

Before increasing swap space, you need to temporarily turn it off. This can be done using the command:

sudo swapoff -a

Step 2: Increasing Swap File Size

There are two ways to increase swap space: by adding a swap file or by extending an existing swap partition. Here, we'll focus on adding a swap file, which is more flexible and less risky.

To create a new swap file, for example, of size 2 GB, use the command:

sudo fallocate -l 2G /swapfile

Set appropriate permissions for the swap file:

sudo chmod 600 /swapfile

Step 3: Formatting the File as Swap

Next, format the file as swap:

sudo mkswap /swapfile

Step 4: Turning On Swap Space

After formatting the file, you can turn it on as swap:

sudo swapon /swapfile

Step 5: Ensuring Persistence of Swap Space

To make the swap space available even after system restarts, you need to add an entry to the /etc/fstab file. Open this file in a text editor:

sudo nano /etc/fstab

and add the following line at the end of the file:

/swapfile swap swap defaults 0 0

Step 6: Setting Swappiness

The swappiness parameter determines how aggressively the system will use swap space. You can set it using the command:

sudo sysctl vm.swappiness=10

For permanent settings, add the line vm.swappiness=10 to the /etc/sysctl.conf file.

 

You should now have increased swap space on your CentOS 7 system. This process can help improve system performance in situations where physical memory is limited. Remember that swap space on disk is slower than physical memory, so it should be used as a supplementary resource rather than a primary memory source.