Skip to main content

How To Add Swap Space on CentOS 7?

We are using high performance SSD VPS where we don't have any SWAP Partition. This can cause you to lose unsaved data or experience downtime. To ensure reliable data access, some applications require swap to function. I have purchase a SSD VPS from Upcloud and get five months Free using Promo Code: AQX767 

My VPS Details:
OS: CentOS Linux release 7.7.1908 (Core)
CPU Model: Intel(R) Xeon(R) Gold 6136 CPU @ 3.00GHz
Storage: SSD 25 GB

Start Your Free Trial for 5 Months, Use Promo Code: AQX767

Swap space can take the form of either a dedicated swap partition or a swap file. In most cases when running CentOS on a virtual machine a swap partition is not present so the only option is to create a swap file. In this guide, we will cover how to create and enable a swap file on a CentOS 7 server.

Before You Begin

Display a summary of swap usage
# swapon --show
If nothing is returned by the command, then the summary was empty and no swap file exists.
Another way of checking for swap space
# free -m
Check Available Storage Space
# df -hT

Creating a Swap File

I will add 4G of swap, if you want to add more swap, replace 4G with the size of the swap space you need. Follow the steps below to add swap space on a CentOS 7 system.
# sudo fallocate -l 4G /swapfile
# sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
# ls -lh /swapfile

# sudo chmod 600 /swapfile
# ls -lh /swapfile

# sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=c8c272bf-6421-4817-85a6-f32414a90b24

# sudo swapon /swapfile
# swapon -s

Make the Swap File Permanent

# vi /etc/fstab

UUID=89c3e8f5-787e-4500-ae7d-6659fa3335df /                       xfs     defaults        1 1
/swapfile                               swap                      swap    defaults        0 0
N.B: Don't use UUID here, it will slow to reboot


The swappiness parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and 100 that represents the percentage of memory usage that will trigger the use of swap.
Values that are closer to 100 will try to put more data into swap in an effort to keep more memory free. See the current swappiness value
# cat /proc/sys/vm/swappiness
CentOS 7 defaults to a swappiness setting of 30, which is a fair middle ground for most desktops and local servers. For a VPS system, we’d probably want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command. For instance, to set the swappiness to 10, we could type:
# sudo sysctl vm.swappiness=10
vm.swappiness = 10
This setting will persist until the next reboot. To make the setting persist between reboots, we can add the outputted line to our sysctl configuration file.
Add your swappiness setting to the bottom of the file:
# vi /etc/sysctl.conf
vm.swappiness = 10

Cache Pressure

Another related value that you might want to modify is the vfs_cache_pressure. This setting affects the storage of special filesystem metadata entries. Constantly reading and refreshing this information is generally very costly, so storing it on the cache for longer is excellent for your system’s performance.
# cat /proc/sys/vm/vfs_cache_pressure

# sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50
Again, this is only valid for our current session. At the bottom, add the line that specifies your new value:
# vi /etc/sysctl.conf
vm.vfs_cache_pressure = 50
Special Thanks to Digital Ocean. This article based on : Link1 Link2 and Link3