What is Swap in Linux?
Swap is memory on a disk that will be used when the amount of physical RAM memory is almost full. It is highly useful if we have low Ram Space because until we have enough RAM Swap will not be touched. Basically, When a Linux system runs out of RAM, unused files will be moved from the Physical RAM to the swap memory and increase the performance of the system. Swap space can take the form of either a dedicated swap partition or a swap file. While installing Linux on the real server we use a swap partition whereas while running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.
Recommended System Swap Space.
If you have 1 GB of (RAM), it’s highly recommended that the swap space should, as a base minimum of 1 GB and a maximum of 2 GB. For more modern systems which have more than 1 GB, your swap memory space should be at a minimum equal to your RAM size “if you use hibernation”, otherwise you need a minimum of half of your RAM and a maximum of twice the amount of RAM. It’s recommended that the swap space is a maximum of twice the amount of RAM depending upon the amount of hard disk space available for the system. The main 2 downsides are to having more swap space than you will actually use, the disk space you will be reserved for it, and memory is too slower than RAM so you can expect the same level of performance boost.
How do I add a swap?
Follow these steps to add 1G of swap to your server. If you want to add more instead of 1 GB, you can do so by changing 1G to your need. 1. Create a file that will be used for swap:
sudo fallocate -l 1G /swapfile
If faillocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
2. Only the root user should be able to write and read the swap file. To set the correct permissions type:
sudo chmod 600 /swapfile
3. Use the mkswap utility to set up the file as a Linux swap area:
sudo mkswap /swapfile
4. Enable the swap with the following command:
sudo swapon /swapfile
To make the change permanent open the /etc/fstab file and append the following line:
nano /etc/fstab
/swapfile swap swap defaults 0 0
5. To verify that the swap is active, use either the swapon or the free command as shown below:
sudo swapon –show
OR
sudo free -h
How to remove Swap File?
If for any reason you want to deactivate and remove the swap file, You can simply follow the steps below:
1. First, deactivate the swap by typing:
sudo swapoff -v /swapfile
2. Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
3. Finally, delete the actual swapfile file using the rm command:
sudo rm /swapfile
Conclusion
Today You have learned how to create a swap file and activate and configure swap space on your Linux system.
If you hit any problems or have feedback, leave a comment below.
Thank you!