Out of RAM: Increase a meagre swapfile under Linux

Swap space is an area set aside for use in the event that physical RAM becomes full. On a Linux machine, when the system runs out of RAM inactive pages are moved to the Swap Space freeing up RAM.

Whilst building a Yocto Image on an old machine with limited RAM I was getting repeated failures during the compilation of qtbase. Analysis of the kernel log revealed that there was a shortage of memory, and hence a need to increase the swap space.

You can determine the current status of the swap space using the following command

$ sudo swapon --show
NAME      TYPE SIZE   USED PRIO
/swapfile file   2G 265.9M   -2

To increase the size of the swapfile, begin by disabling the swap space, and then remove the existing swapfile.

sudo swapoff /swapfile
sudo rm /swapfile

You can now create a new blank swapfile, set the permissions associated with it, and then re-enable the swap space.

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile