How to add or remove swap space

Swap space refers to the portion of the virtual memory which is reserved as a temporary storage location.

Check memory and available space

$ free -mh
$ df -h

Enable the swap file

# create a 2G swap file
$ dd if=/dev/zero of=/myswap bs=1024 count=2048000
# mark the file as swap space
$ mkswap /myswap
# enable the swap file
$ swapon /myswap

Make the swap file permanent

$ echo '/myswap none swap sw 0 0' >> /etc/fstab

Disable the swap space

$ swapoff /var/swap
$ rm /swap

Then you should remove the line includes ‘/swap’ from /etc/fstab

References

[1] How To Add Swap Space on Ubuntu 16.04