When a system runs out of memory, the operating system will begin to swap or page out memory pages to persistent storage such as a disk drive. This is because virtual memory is faster than physical memory and it’s cheaper to store data on disk rather than RAM. When you have more free disk space, you can add additional swap space so your OS has an additional location to store temporary data when necessary. If your server does not have enough physical memory for all the processes that need it, some of them may be forced to use virtual memory rather than physical memory. This means that they are going to be slower and use disk storage as a “scratchpad” for their operational data. Where possible, this should be avoided as it is a less efficient use of resources. But if it becomes necessary, then you can add swap space on CentOS (or any other Linux system).

Advertisement

How to Add Swap Space in linux

The following steps will help you to create swap space on your CentOS, RHEL, and Scientific Linux systems.

  1. Check System Swap:

    First of all, check if any swap space is already configured on your system. If there is no swap, you will get the output header only.

    sudo swapon -s 
    
  2. Create a Swap File:

    The general rule of thumb is to add about twice the amount of physical memory. But this is a very rough estimate and does not account for the varied workloads of different systems. A more accurate formula is to multiply the amount of RAM by 2, then add 10% for good measure. For example, if you have a server with 16 GB of RAM, you should add 32 GB of swap. That’s because 4 GB * 2 = 32 GB + 10% = 34 GB.

    fallocate -l 8G /swapfile 
    

    Set the proper permissions on the file.

    chmod 600 /swapfile 
    
  3. Make It Swap:

    Now, use the mkswap to create the swap area on the above-created file. Once you did it, the Swap file is ready to work as Swap space on your system.

    mkswap /swapfile 
    
  4. Enable Swap Space:

    As of now, you have created a swap space in a file. Next is to use the swapon command to enable devices and files for paging and swapping.

    swapon /swapfile 
    

    Your system will start using the swap space now. You can verify this by running the following command:

    sudo swapon -s 
    
    Filename                Type        Size     Used    Priority
    /swapfile               file        1043340  881068       -2
    
  5. Setup Swap Permanent:

    The above instructions enabled the Swap temporarily, which will be lost after a system reboot. To enable this permanently, append the below-given entry in /etc/fstab file.

    sudo vim /etc/fstab 
    

    Append the below line to the file:

    /swapfile   none    swap    sw    0   0
    

    Save the file and close it.

  6. Setup Kernel Parameter:

    Now change the swappiness kernel parameter as per your requirement. It tells the system how often the system utilizes this swap area.

    Edit /etc/sysctl.conf file and append following configuration in file.

    sudo vim /etc/sysctl.conf 
    

    Set the vm.swappiness paramenter as below:

    vm.swappiness=10
    

    Now reload the sysctl configuration file

    sudo sysctl -p 
    

Conclusion

When a system runs out of memory, the operating system will begin to swap or page out memory pages to persistent storage such as a disk drive. When you have more free disk space, you can add additional swap space so your OS has an additional location to store temporary data when necessary. The general rule of thumb is to add about twice the amount of physical memory. But this is a very rough estimate and does not account for the varied workloads of different systems.

A more accurate formula is to multiply the amount of RAM by 2, then add 10% for good measure. And that’s when you’ve run out of physical memory and can’t service any new requests. When this happens, the OS swaps out the least recently used (LRU) processes and pages their data to the disk. With enough swap space, you can avoid reaching this situation.

Share.

1 Comment

Leave A Reply

Exit mobile version