What is SWAP space?
SWAP space in Linux is used when the physical memory of system gets full. If the system needs more memory resources and RAM is full then memory is moved to SWAP space. But SWAP cannot be used as a RAM replacement.
What is the benefit of SWAP space?
The main benefit of SWAP to save the system from get crashed, system may get slow but your application won’t crash.
How to add SWAP space?
Run the below commands to add SWAP:
- Create swap file:
sudo fallocate -l 1G /var/swapfile
- Change the file permission so that only owner can read and write:
chmod 600 /var/swapfile
- Mark the file as swap space:
sudo mkswap /var/swapfile
- Enable the swap file:
sudo swapon /var/swapfile
- To make it permanent so that swap settings will stay even if we reboot, edit fstab file:
sudo vi /etc/fstab
- Add the below line in fstab file and save it:
/var/swapfile swap swap defaults 0 0
- To verify swap is added and enabled use below command:
sudo swapon -s