How To Add Swap Memory to CentOS 7
In this opportunity, we will see how we can increase the swap memory in computers with CentOS 7 .
What is SWAP?
The swap memory can be defined as the virtual memory that supports the main memory, the RAM, of the computer. It is basically an exchange space which can be a file or a logical partition on the hard disk.
The swap memory stores all the temporary files on the hard disk which allows the performance of the RAM memory to be optimized.
Checking Swap Memory in the System
Before starting the memory expansion process it is advisable to check the status of the swap memory of the system using the command:
In case that we do not obtain any result it means that we do not have any swap file created. Another way we can get detailed information about RAM memory and swap memory is by using the command:
There we can see the total of the memory and the amount that we have used.
Check Available Space in CentOS 7
The next step we must take is to verify the amount of space that we have available since, as we have mentioned, the swap memory can be a logical partition within the disk.
To validate the available space we will use the command:
Create the swap file
The next step in this process is to create the swap file. For this we will create a file called swapfile in the root/.
This file will indicate the amount of memory that we want to assign as an exchange space.
The simplest and most practical way to create this swap file is using the command fallocate, thus, if we want to create a file of 1 GB of space we will enter the following:
sudo fallocate -l 1G /swapfile
We can verify that the indicated amount is correct using the following command:
ls -lh /swapfile
We can see the properties, the user, the amount of space allocated and the date of creation of the file.
Enable the swap file
Up to this point we have created the swap file but it is necessary that it be enabled for the system to recognize it as a swap file, otherwise we have not done anything.
The first step is to assign the permissions in the file, using the chmod command , so that nobody but the root user can access and make changes to the swap file. We will enter the following:
sudo chmod 600 /swapfile
We can validate that the permissions have been established correctly using the line again:
ls -lh /swapfile
Now that we know that the file has the required permissions we must indicate to the system that the created file will be a swap file, for this we enter the following:
sudo mkswap /swapfile
Allow the created file to be permanent
Then we must configure the swap file so that it becomes permanent since the next system restart will not be active, for this we will use the fstab command which is responsible for managing the tables and file systems.
We will enter the following command to access the file:
sudo nano /etc/fstab
Once we execute this command we will see the following:
There we must add the following at the top of the file:
/swapfile swap sw 0 0
Save the changes using the key combination Ctrl + O and left the editor using the combination Ctrl + X .
There are some parameters that we can adjust so that the operation of the swap file is the best and note the performance in the system.
The values that we can configure are:
This parameter is responsible for determining the frequency with which the CentOS 7 system exchanges the data from the memory for the swap space.
To see the swappinees we will enter the following:
We recognize that the value, in this case is 30. The closer the value is to zero (0) the system will only exchange the data in extreme case. If we want to adjust this value we will enter the following with the desired amount:
sudo sysctl vm.swappiness=5
This change will be reflected in the next reboot of the system. Additionally, we must edit the sysctl.conf file if we want the modified value to be permanent:
sudo nano /etc/sysctl.conf
Once the file is opened, we will add the following at the top:
vm.swappiness = 5
Another one of the values that we can modify is that of Cache Pressure , vfs_cache_pressure . This parameter is related to the special entries of the metadata file system.
To see the current value of Cache Pressure we will use the following command:
We recognize that the default value is 100. With this value, the system will eliminate the information too quickly, which is why it is advisable to set a lower value, for this we will use the following command:
sudo sysctl vm.vfs_cache_pressure=35
(Assuming we want to leave it in 35)
As in the previous parameter, we must configure the sysctl.conf file so that this change is permanent. There we will enter the following line:
vm.vfs_cache_pressure = 35
Using these commands, we can add swap memory to our CentOS 7 system and optimize the performance of it.