Before You Begin
check if your CentOS installation already has swap enabled by typing:
swapon –show
Empty output means that your system does not have swap enabled. Otherwise, swap enabled. Note that you may have multiple swap partitions and/or files.
Creating a Swap Partition
Creating a Swap File
First, create a file which will be used as swap space 1GB:
fallocate -l 1G /swapfile
If the fallocate utility is not available
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Ensure that only the root user can read and write the swap file
chmod 600 /swapfile
Next, set up a Linux swap area on the file:
mkswap /swapfile
Run the following command to activate the swap:
swapon /swapfile
Make the change permanent by opening the /etc/fstab file:
vi /etc/fstab
and pasting the following line:
/swapfile swap swap defaults 0 0
Verify that the swap is active
swapon –show
or
free -h
Adjusting the Swappiness Value
Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping
cat /proc/sys/vm/swappiness
To set the swappiness value to 10
sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
vm.swappiness=10
Removing a Swap File #
Start by deactivating the swap space by typing:
sudo swapoff -v /swapfile
Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
Finally, delete the actual swapfile file with rm :
sudo rm /swapfile