Swap space is a space allocated on the local hard drive that is used when physical memory becomes full. It is also known as Virtual RAM on Windows. The swap space can be allocated as a separate partition or a swap file on the existing system partition. Since in most of the cases, partitions are predefined, hence swap file is considered more appropriate.

Check If Swap Is Already Enabled

swapon --show

If the output is empty, the system does not have swap space enabled. If you have something as shown below, the swap is already enabled

[email protected]:~# swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

Creating Swap File

Step One – Create a swap file

fallocate -l 1G /swapfile

Here we are creating a swap file of 1GB. You can change the size according to your requirements and available space. In case the fallocate utility is not installed, you will receive an error “fallocate failed: Operation not supported”, then alternatively use the following method to create a swap file.

dd if=/dev/zero of=/swapfile bs=1024 count=1048576 

Step Two – Make sure that only the root user has access to the file so that the file doesn’t get corrupted by other users.

chmod 600 /swapfile

Step Three – Set up a Linux swap area on that file

[email protected]:~# mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=65eb6626-e374-484c-b05c-ef1bb58c67a2

Step Four – Activate the swap file

swapon /swapfile

Open the /etc/fstab/ file using:

nano /etc/fstab

And paste the following line.

/swapfile swap swap defaults 0 0

So that it looks similar to the following file:

/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/vda1 during installation
UUID=acb898d9-d5f2-4541-bdaa-9913acc386c6 / ext4 errors=remount-ro 0  1   

/dev/fd0        /media/floppy0  auto  rw,user,noauto,exec,utf8 0 0

/swapfile swap swap defaults 0 0

Step Five – Verify the swap space using the following command

[email protected]:~# swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

OR using

[email protected]:~# free -h
          total     used     free    shared     buff/cache    available
Mem:       481M      91M      24M      636K            364M        376M
Swap:      1.0G       0B     1.0G

Adjusting The Value Of Swap

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Its value can be adjusted between 0 to 100. A low value will make the operating system to try to avoid swapping whenever possible while a higher value will make the operating system to use the swap space more often.

The default value usually is 30. You can check the current swappiness value by typing the following command:

[email protected]:~# cat /proc/sys/vm/swappiness
60

To change the value of swapiness, lets suppose to 20, use the following command:

[email protected]:~# sysctl vm.swapiness=20
vm.swappiness = 20

To make this parameter permanent even after rebooting the system, open up the  /etc/sysctl.conf file and append the following line

/etc/sysctl.conf
net.ipv6.conf.all.accept_ra = 2

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

vm.swappiness=10

Deleting Swap File

Step One – Deactivate the swap space by typing

swapoff -v /swapfile

Step Two – Delete the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/vda1 during installation
UUID=acb898d9-d5f2-4541-bdaa-9913acc386c6 / ext4 errors=remount-ro 0  1   

/dev/fd0        /media/floppy0  auto  rw,user,noauto,exec,utf8 0 0

Step Three – Delete the actual swap file

rm /swapfile