Add a Swap file

  • GNU/Linux


We will see how to increase swap memory using a file. Swap allows to unload RAM when it is saturated and it is generally located on a partition, we then speak of swap partition. It is possible to use a file rather than a partition and this offers the advantage of not manipulating partitions, adding or deleting them on demand. To avoid problems, you will have to create the swap file on the root. Indeed, the swap is mounted just after the root and before the other filesystems. To know the current amount of swap you can use the free command.

fallocate -l 1g /my_swap
chmod 600 /my_swap
mkswap /my_swap
swapon /my_swap

You can check if it works with the commands:

cat /proc/swaps
free -h

To make the change persistent, add the file to /etc/fstab.

/my_swap swap swap defaults 0 0