How to increase Swap space in Linux

Swap space is a space on the hard disk which is used as a substitute for physical memory. Whenever the computer is running out of physical memory(RAM), the operating system moves inactive apps and pages into Swap space. This frees up space on the physical memory(RAM) that can be used by other applications.

Software and applications are becoming more and more demanding day by day. If you are using a PC that is low on physical memory than its important to have a swap space around. If you already have a swap space and you are still facing lagging issues then it’s important that you increase the swap space. This article will show how you can create Swap space, increase swap space, and even remove swap partition.

Increase swap space using swap file.

Do you need Swap space?

Not necessarily, if you have a high amount of RAM compared to the applications you use then you don’t need swap space. You can check this by opening the task manager when using pc. If RAM is being used more than 90% at any given time than you need to have Swap space. If you already have swap space and is being used more than 90% at than you should increase it.

Check current Swap space in Linux

To check swap partition of your system type command free -m or swapon -s. Here free -m shows memory in MB while swapon -s will show memory in KB. This command will show swap space if any with its size.

You can create both a swap file or a swap partition in your Linux system. If you already have a swap partition then you can create a swap file to increase swap space in Linux.

Create a Swap partition in Linux

For creating a swap space you need to have free partition on your hard drive.

Type the command below in the terminal to check the current partitions in your Linux.

sudo fdisk -l

Note down the device name of the hard drive that you want to use as a swap. Then type the command below in terminal and press enter. (obviously, replace XX with the device name of your drive.)

sudo swapon /dev/sdXX

To make this swap space partition available even after a reboot, add the following line to the /etc/fstab file.

sudo nano /etc/fstab

In file /etc/fstab type the line below.

/dev/sdXX  swap  swap  defaults  0 0

This completes the creation of swap space partition. Just verify this by typing command free -m, if you see Swap: then you have successfully created a swap partition.

Create a Swap file to increase swap space

If you don’t have a free partition available on your hard drive or if you don’t want to create a separate partition then follow these steps to create a swap file to increase your swap space.

Step 1: Create a Swap file using dd command

# dd if=/dev/zero of=/swap_file bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 414.898 s, 2.6 MB/s
#

Here replace the size of bs according to the size of swap file you require.

Step:1 Secure the swap file with permissions.

# sudo chmod 600 /swap_file

Step:2 Use mkswap command to make this file as Swap

# sudo mkswap /root/myswapfile

Step:3 Enable the newly created Swap file

# sudo swapon /root/myswapfile

Step:4 Make this Swap file available even after a reboot

# sudo swapon /root/myswapfile

Type the line below in /etc/fstab file.

/swap_file  swap  swap  defaults  0 0

Verify the newly created swap file by free -m and swapon -s. You should now see the file /swap_file.

How to remove Swap space in Linux

To remove the swap file first you need to deactivate swap file.

sudo swapoff -v /swapfile

Type the above command in terminal and it will deactivate swap file.

Now remove the entry of swap file in /etc/fstab.

sudo nano /etc/fstab

Now delete the line /swapfile swap swap defaults 0 0 inside fstab file. When done type below the below command and press enter.

sudo rm /swapfile

This command will delete the swap file.

We hope you like this post. If you face any problems please do tell us in the comments. It would be a great help from your side when you provide us your valuable feedback in comments. Do you have an old pc lying around, then here are the most lightweight Linux distros that could give a new life to old PC’s.

Leave a Reply

Share via
Copy link