How to increase Swap size on CentOS + cPanel

Today you will learn how to increase Swap size on Linux systems, this applies to CentOS Linux and cPanel based servers.

Sometimes our swap memory size is too small, and once your swap is totally consumed, you will face really slow speeds on your server because your RAM + Swap will be fully used.

But hey, what’s Swap memory after all?

Swap memory is a disk based memory that is used when the RAM memory is full. Once you reach the total amount of RAM, Linux will automatically switch and start using SWAP memory, then the inactive pages in memory will be moved to SWAP. One important thing to consider: swap is not the same as RAM, swap is based on disk space (SATA, SAS or SSD), which is slower than RAM, so keep in mind that once SWAP is used, you may see a slow down in your system performance.

Swap memory is

How can I increase Swap size on cPanel and CentOS Linux?

We can increase 4GB of swap size now.

Create Swap file

touch /newswap

Use the following command to create 4GB of swap file.

dd if=/dev/zero of=/newswap bs=1M count=4096

This command will create a 4GB Linux partition. In order to convert this partition into a Swap partition, you’ll need to use the mkswap command, as you see below:

mkswap /newswap

Alright, your new swap partition is ready to be active swap, we will do that using the command ‘swapon’, check it out:

swapon /newswap

Finally, in order to have your swap memory automatically loaded after reboot, you will need to add an entry to your /etc/fstab file, like this:

nano -w /etc/fstab

Then add this content:

/newswap swap swap defaults 0 0

Save and close the file.

How can I verify if my Swap is detected and used by the Linux system?

Simple, use the ‘free’ command:

free -m

You should get something like:

[[email protected]:~]free -m
total used free shared buffers cached
Mem: 24001 19118 4883 6 638 1697
-/+ buffers/cache: 16781 7219
Swap: 7999 185 7814 

The last line shows all the values of your SWAP Memory (total swap memory, swap used and free swap left)

How can I remove my SWAP memory?

First, deactivate your SWAP using ‘swapoff’ command, like this:

swapoff /newswap

Remove your Swap entry from /etc/fstab file

nano -w /etc/fstab

Remove /newswap file using ‘rm’ commnad, as you see below:

rm /newswap -fv

Conclusion

As you see, it’s pretty easy to increase Swap size on CentOS and cPanel servers, it should take about 1 minute and then you are ready to go. Remember that using SWAP is a sympthom of lack of RAM, so, it’s always recommended to have plenty of RAM because Swap is a slow type of memory and never recommended for high demanding applications.

Further reading: All about Swap memory

About the Author: Santiago Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *