I found the following video very interesting and a straightforward approach to increase the size of swapfile. Checkout this short video tutorial: How to Increase Swap on Ubuntu Linux | Linux Handbook
(From this video) following is the way to increase the swap space (if your system is using swapfile):
First check the swap size of your linux system:
swapon --show
Sample output:
NAME TYPE SIZE USED PRIO
/swapfile file 797.2M 0B -2
This output shows the swap size is being used and the type of swap. Here the swap type is "file" type (it could be partition type as well).Ubuntu uses swapfile by default and increasing and decreasing the size of it is fairly easy.
As we are going to modify the swapfile so at first turn it OFF. You might notice the process is being killed for multiple times. This happens because before turning the swap OFF the system moves its contents to main memory which may overflow the main memory. So, if the following command is killed, run it multiple times until it is successfully completed. You may also face sudden logout and / or other processes may be killed automatically.:
sudo swapoff /swapfile
Now if you run swapon --show it won't show anything as it is turned OFF.
Now change the swap size (I am making it 4GB, for n GB make it nG):
sudo fallocate -l 4G /swapfile
Now if you check the file you will see the size is increased. To see it:
ls -lh /swapfile
Now tell the Linux system to use this file as swap system:
sudo mkswap /swapfile
This will wipe the old swap signature and use the new one.
Now to turn the swap ON (as we initially turned OFF the swap space):
sudo swapon /swapfile
Done!
Now use swapon --show or free -h to see the change.