How to Increase Swap Space in Linux?

The swap memory is the physical memory which is not a replacement of the RAM, but it is the substitute of the RAM, which ensures the running processes should not be halted in Linux. When your computer memory is fully occupied, then a few inactive pages are moved to the swap space from the RAM to vacate some space for other processes. There might be a time when you need to increase the swap space of your Linux system.

This post will list the possible methods to increase Swap Space in Linux:

How Much Space Required With the Different RAM Memories?

There is no hard and fast rule for assigning the swap memory with the actual RAM. But it is recommended to have the minimum swap space with the specified RAM as shown in the table below:

Distribution(s)System RAMSwap Space
For Debian/Ubuntu-based DistributionsLess than 1 GBShould be greater the RAM memory, but less than double of RAM
Greater than 1 GBShould be greater than the square root of RAM, but less than the double of RAMDouble of RAM
For CentOS and RedHat basedLess than 2 GB
2 GB – 8 GBEqual to RAM
8 GB – 64 GBMinimum 4 GB
Greater than 64 GBMinimum 8 GB

How Do I Increase Swap Space in Linux?

There are two different methods to increase the Swap Space in Linux:

  • Create a Swap File
  • Create a New Partition

Method 1: Increase Swap Space By Creating a Swap File

Before increasing the swap file, let’s check the current swap space on the system using the “free -h” command: 

$ free -h

The free space is 3GB.

The step followed below will be followed to increase swap space. 

Step 1: Create a Swap File

Run the below-mentioned command to assign the swap space: 

$ sudo dd if=/dev/zero of=/swap bs=1GB count=1

The command is described as: 

  • The “/swap_file” represents the location of the swap file being created.
  • The “bs” is the set of block size, and the “count” are the number of blocks.

Step 2 (Optional): Make the Swap File Secure 

Then, make the swap file secure and grant only the owner can read and write: 

$ sudo chmod 600 /swap

Step 3: Enable the Swap Area 

Enable the swap area by using the command which will setup all the prerequisites to using the swap space:

$ sudo mkswap /swap

Step 4: Enable the Swap File 

Once the swap area is created on the drive, just run the command to make it usable: 

$ sudo swapon /swap_file

Verify the Creation

Verify the swap file has been increased or not:

$ free -h

The swap space has been increased successfully. 

Method 2: Increase Swap Space By Creating a Partition

Another method to increase the swap space is to create a new partition. The steps mentioned below will explain how to increase the Swap space by using the new partition.

Step 1: Create a new Partition 

Use the gdisk utility to create a new partition for a swap file. For this, use the disk name that you want to use, i.e., (/dev/sda) in our case. 

$ sudo gdisk /dev/sda

When the command is executed, you will be required to press “n” to create a new partition, then the partition number, and at the end, the “w” command will write the changes. 

Lastly, press “y” to successfully terminate the partition creation: 

Step 2: Reload the Partition Table 

After that, reload the partition table of the disk so that the system can adapt the new changes: 

$ sudo partprobe /dev/sda

Step 3: Create a Swap Space 

Now, create the Swap space on the newly created partition (using the disk name alongside the partition number (4 in our case), ):

$ sudo mkswap /dev/sda4

Step 4: Add the Swap Info to the /etc/fstab file 

Now, use the UUID of the swap space and add the entry into the “/etc/fstab” file. For this, access the “/etc/fstab” file via any text editor: 

$ sudo nano /etc/fstab

Using the UUID, the following line is added in this case:

UUID=4cc68efe-4d07-4131-9004-7192cd3c9014     swap        swap        defaults,_netdev,x-initrd.mount    0  0

Step 5: Load the Swap Space 

Finally, load the swap space to make it useful using the command: 

$ sudo swapon -a

Verify the Swap Space 

Verify the newly added swap space via the command written below: 

$ sudo swapon -s

The output shows that the “/dev/sda4” is added as a swap space with a size of 1000KB. 

Conclusion

To increase the swap space in Linux, we can create a new swap file or a new partition. Before doing so, ensure that you are following the proper instructions as per your Linux distribution and RAM space availability. This post has listed the possible ways to increase swap space in Linux.