When a Linux system is initially set up, a swap partition is created on the hard drive that will be used as virtual memory in Linux, along with other partitions used for data. Unfortunately, partitions on the hard drive cannot be re-sized without the loss of data. However, not all is lost if more swap is needed. If there is remaining space on a file system that can be used as swap, a swap file can be created that is used exclusively as additional virtual memory in Linux.
Determine the amount of free space available with the "df" command. Decide upon the size of the swap file based upon the amount of free space.
Create a swap file of the size decided upon earlier with the command "sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=1024" where 1024 is the size of the swap file in megabytes and the full name of the swapfile is /mnt/swapfile.
Format the swap file with the command "sudo mkswap /mnt/swapfile."
Enable the swap file with the command "sudo swapon /mnt/swapfile."
The new swap file will not be used after a reboot unless the system is told to use the swap file. Edit the /etc/fstab file and add the following line in /etc/fstab to make the swap file persistent between reboots "/mnt/swapfile none swap sw 00."
Once the swap file is no longer needed, remember to turn off the swap file, remove the swap file entry in /etc/fstab, and finally remove the swap file.