The Linux kernel divides its physical RAM into memory layers called pages. Sometimes these pages need to be copied to an area of the disk to free up RAM. This process is known as swapping, and the space where the page is copied is known as swap space or the swap area.
All GNU/Linux images, including Debian, Centos and Ubuntu, are configured with the swap area mentioned above.
Generally, this disk space is normally a disk partition that is usually added at the end, but it can also be a file. At Clouding we have chosen to use a swap file or swapfile of 128 MB at most. A file allows its size to be changed on the fly and is easier to remove completely.
We use a swapfile instead of a partition because it is more flexible for the end user. This way, users can make the file larger if needed without having to enlarge a partition.
free -hAs shown in the image above, we can see that we have a swap file of 128 MB and, in the next image, that it is located at /swap/swapfile.
cat /etc/fstab | grep swapAnd we can see that the file is mounted during startup as a swap area.
Create a larger swap file
If you want a swapfile with a larger size, simply follow the steps below.
Create the file that will serve as the swapfile and use the -l (lenght) parameter to specify how many MB you want this file to be:
fallocate -l 512M /swap/newswapfileOr alternatively:
dd if=/dev/zero of=/swap/newswapfile bs=1M count=512Set the correct permissions for a swap file:
chmod 600 /swap/newswapfileAfter creating the swap file with a specific size, you need to format it as swap:
mkswap /swap/newswapfileAnd activate the new swap file:
swapon /swap/newswapfileDisable the swapfile previously used:
swapoff /swap/swapfileFinally, modify the entry in /etc/fstab:
/swap/newswapfile swap swap defaults 0 0Once all these steps have been completed, you can display the summary of swapfile usage:
swapon -s