By
Vineel Sai
Swap is a portion of the hard disk that is used by the operating system when to store temporary data when the system is running out of memory. It basically works as an extension of the RAM but the data is stored on a disk instead of RAM, but don't think it is a replacement for RAM because hard disk or even SSDs are much slower than RAM, so the swap is just to prevent the system from crashing if it runs out of memory.
sudo swapon --show
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Swappiness is a value that determines how much memory is swapped out to disk. It is a value between 0 and 100.
Run the following command to see the current swappiness value, it is usually set to 60.
cat /proc/sys/vm/swappiness
It is not bad if you have a high speed SSD or SSD with a lot of cache, but if you have a slow HDD then it’s better to set the swappiness closer to 0(but not 0).
Run this command to set the current swappiness to 10.
sudo sysctl vm.swappiness=10
sudo nano /etc/sysctl.conf
vm.swappiness=10