How To Enable Swap in Ubuntu
In Ubuntu (and other operating systems), "swap" refers to a portion of the hard drive that is set aside as virtual memory. When the system is running low on physical memory (RAM), it can temporarily move some of the data from RAM to the swap space, allowing the system to continue running without crashing. This can be useful in situations where you are running many programs at once or have a system with a low amount of RAM.
The swap space can be a separate partition on the hard drive, or it can be a file within a partition. It is generally recommended to have some swap space available, especially on systems with low amounts of RAM, as it can help prevent out-of-memory errors and improve system performance. However, it is also important to ensure that there is sufficient physical RAM available, as accessing the swap space is generally slower than accessing RAM.
RAM Vs Swap
RAM (random access memory) and swap space are both types of memory used by a computer to store data. The main difference between the two is that RAM is volatile memory that is used to store data that the computer is currently using or processing, while swap space is a non-volatile memory that is used to store data that is not being used currently but may be needed in the future.
RAM is much faster than swap space, as it is located on the motherboard and can be accessed directly by the computer's central processing unit (CPU). In contrast, swap space is usually located on the hard drive or a separate partition, and accessing it is slower than accessing RAM because the hard drive is slower than the motherboard.
When the system is running low on physical RAM, it can temporarily move some of the data from RAM to swap space to free up space in RAM. This is known as "swapping." While swapping can help a system with low amounts of RAM to continue running without crashing, it can also slow down the system as it takes longer to access data in swap space than in RAM.
In general, it is best to have enough RAM to meet the needs of your system and avoid relying too heavily on swap space. However, having some swap space available can be useful in situations where you are running many programs at once or have a system with a low amount of RAM.
Enabling Swap
Following is the code to Enable 2GB Swap.
Change 2 to the amount of Swap space you want. A general rule of thumb is to have a swap of 50% of your RAM
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
#Permanently Enable
sudo nano /etc/fstab
/swapfile swap swap defaults 0 0
Swap is not an alternative to Less RAM consider it more like an Emergency RAM.
SWAP really works well when you are running your Storage on SSD.