Add Swap Space on CentOS 7

How to Add Swap Space on CentOS 7 in Google Cloud

If you’re using a virtual machine instance with limited RAM on Google Cloud, you may run out of memory when running resource-intensive applications. To prevent this, you can add swap space to your CentOS 7 operating system. Swap space is a reserved area of disk space that can be used as an extension of physical memory.

When the available RAM is full, inactive pages are swapped to the disk to free up memory for active processes. This tutorial will guide you through the steps to add swap space on CentOS 7, ensuring smooth and efficient operation of your VM instance.

In this tutorial, you’ll learn how to add swap space to a CentOS 7 system. Follow the steps below to increase the available memory on your server and ensure optimal performance.

Check Swap Space

Before adding swap space to your CentOS server, it’s important to check whether it’s already available. By default, many virtual machines do not have swap space enabled. To check if your server has swap space, use the following command:

sudo swapon --show

If you don’t receive any output, it means that your server doesn’t currently have any swap space enabled. In this case, it’s necessary to add swap memory to your system to prevent running out of memory during resource-intensive operations.

Creating a Swap File

To add swap space to your CentOS server, you can create a swap file with a specified size. In this example, we will create a 1 GB (1G) swap file, but you can adjust the size as needed to suit your requirements.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048572

Set up Swap File Permissions

Once you have created the swap file of the correct size, it can be enabled as swap space.

However, before doing so, it’s important to ensure that the correct permissions are set for the file.

sudo chmod 600 /swapfile

Set up a Swap Space

To mark the file as swap space and prepare it for use, you can run the ‘mkswap’ command with the path to the swap file that you created in the previous steps.

sudo mkswap /swapfile

After running the ‘mkswap’ command, you should see output similar to the following:

Setting up swapspace version 1, size = 1048568 KiB
no label, UUID=83500984-b857-4ed3-b46c-c3b68c0e5272

Enable Swap Space

After setting up the swap file and marking it as swap space, the final step is to enable it and allow the system to utilize the additional space for memory management.

sudo swapon /swapfile

To confirm that the swap space is now available and enabled, you can use the following command:

sudo swapon --show
/swapfile file 1024M 0B -2

To verify that the swap space is active and in use, you can run the ‘free’ utility again and check the output.

free -h
           total     used    free    shared   buff/cache  available
Mem:       587M      126M    48M     4.3M     412M        339M
Swap:      1.0G      0B      1.0G

Making the Swap File Permanent

Install nano nano editor.

sudo yum install nano -y

Note that the swap space you created in the previous steps will only persist for the current session. To ensure that the swap file is available after a reboot, you can add an entry for it in the ‘/etc/fstab’ file.

sudo nano /etc/fstab

To add the swap file to the ‘/etc/fstab’ file, you can append the following line to the end of the file:

/swapfile swap swap defaults 0 0

Configure Swappiness Value

The ‘swappiness’ parameter in CentOS allows you to configure how often the system swaps data from RAM to the swap space. The value of this parameter ranges from 0 to 100 in percentage. A value closer to zero indicates that the kernel will not swap data to the disk unless it is absolutely necessary.

On the other hand, a higher value closer to 100 will attempt to move more data into swap to keep more RAM space free.

Setting a lower swappiness value can generally improve system performance by reducing reliance on the swap. This value, expressed as a percentage between 0 and 100, determines how often the kernel swaps data out of RAM to the swap space.

A value closer to 0 means the kernel will avoid swapping data to disk as much as possible, while a value closer to 100 will try to keep more RAM free by putting more data into swap. To check the current swappiness value, run the following command:

cat /proc/sys/vm/swappiness

Now set the value to 10 with the following command.

sudo sysctl vm.swappiness=10
vm.swappiness = 10

You can make this change permanent by adding the following line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

At the bottom, add the following line.


To save and close the file, press Ctrl+X, then Y, and finally Enter.

Removing Swap Space

To remove a swap space, you must first disable the space by using the «swapoff» command. Next, remove the entry from the /etc/fstab file, and finally, delete the swap file.

sudo swapoff -v /swapfile

You can remove the swap file entry «/swapfile swap swap defaults 0 0» from the /etc/fstab file, and then proceed to delete the swap file.

sudo rm /swapfile


Congratulations, you have successfully learned how to create, activate, configure, and remove swap space on your CentOS 7 server running on Google Cloud. Thank you for taking the time to read this guide. If you have any questions or feedback, please feel free to leave a comment below.

Похожие записи

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *