Linux

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.

Akash

Recent Posts

Linux Software must have to improve Productivity

You can utilize a wide range of tools as a Linux user to keep organized, manage your time, and become… Read More

2 years ago

How to get Complete details of an Audio Video File

Have you ever caught yourself in a situation where you need details of an audio/ video media file? When you… Read More

3 years ago

Fix Windows 10 showing wrong time in dual boot with Linux

If you have installed Linux alongside windows in the dual boot you might have noticed that every time you boot… Read More

3 years ago

How to update Linux Kernel to the latest version easily

Linux kernel is like the core of the operating system. It's a piece of software that works as a middle… Read More

4 years ago

3 Methods for How to Install Python on Linux System

Nowadays, most Linux Distros already have Python preinstalled. But you might have a distribution that might not have Python preinstalled.… Read More

4 years ago

How to Install Python on Windows 10 to begin coding

Python is one of the most popular and easy to learn programming languages. Its a widely used high-level programming language… Read More

4 years ago

Just so that you know we use cookies on our website. Want to know more

read here