How to Create a Custom Sized Swap File in Linux?

If you are using old hardware to run any Linux Distribution which has a very low system memory (RAM), it is recommended to create a swap partition. A Swap partition is essentially a part of your disk storage that is used by the system as ‘Extra RAM’. This extra disk space starts swapping the RAM if it ever gets full. And once the RAM is free, the disk transfers all the processes back to the Memory stick.

A swap file is considered a better option when compared to the swap partition, because you can easily resize the file in the future when needed, and you won’t have to go through the hassle of resizing all the partitions using Gparted.

Read more about Linux swap before you go further if you do not know what swap is.

Why should you use swap?

There are a number of reasons for using swap, whether in the partition form or in the Swap file form. Some of them are:

  1. If you have RAM less than or equal to 4 Gb, then it is recommended to create a swap.
  2. If you are going to Hibernate your PC ever, then you should create a swap space equal to the RAM installed on Your PC.
  3. Even if you have RAM such as 32 or 64 GB, it is still recommended to create some Swap space even if you won’t utilize it, it’s better to be on the safe side.

Check if swap is already available on your system

Many distributions automatically create a Swap file or Swap partition when you install them. So you should probably check if there’s any swap already available. Type the following commands in your Terminal :

free -h
Swap Is Absent In My PC
Swap Is Absent In My PC

If the Swap is available on your machine, then you should check whether it is a swap file or a swap partition. Ypou can check it with the following command :

swapon --show

Creating a Swap File in Linux

For demonstration purposes, I’m going to create a file of 2 GB, but you can create it of a size of your preference. Type the following in your Terminal Window :

sudo fallocate -l 2G /swapfile
Creating A Swap File
Creating A Swap File

We also have to change the permissions for the swap file so that only admin has the right to read or write. You can change the permission using chmod command :

sudo chmod 0600 /swapfile

Now, you have to tell your Operating System that you have created a swap space and that your system should start using it. You can do this by typing the following command :

sudo mkswap /swapfile
Give Appropriate Permission And Create Swap
Give Appropriate Permission And Create Swap

Finally, you just have to turn the swap file on. You can do that by typing :

sudo swapon /swapfile

Now, if you type free -h, you can see that we’re using swap.

System Is Using The Swap File Now
System Is Using The Swap File Now

Now all that’s left is that we have to make the above changes permanent. We have to modify the fstab file now. Type the following commands :

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Your Fstab File Should Look Like This Now
Your Fstab File Should Look Like This Now

Summary

Swap is obviously slower than RAMs and therefore it is not recommended on higher end PCs to use swap more than 1 or 2 GB. You can also change the swappiness (How aggressively swap is used) in your system by adding a line in /etc/sysctl.conf file :

vm.swappiness=10

Swappiness has a range between 0 and 200, and it is recommended to have a lower value to avoid aggressive swapping.

Reference

Swap – ArchWiki