disable swap in linux

How to Disable Swap in Linux

Swap space is a specific part of hard disk used to extend RAM memory of a system. When you run a task, process or application, it is loaded into RAM memory. But when your system uses too many applications or processes, its RAM becomes full. In such cases, your operating system will unload some of your inactive applications/processes onto the swap space designated on your system’s hard disk, to free up RAM and load new applications. But since swap space is on hard disk, it is much slower to access information from it, compared to doing it on RAM. So if there is too much swapping on your system, it will slow down performance. On the other hand, some systems have so much RAM that they don’t need a swap space. In both these cases, you can disable swap space in your system. In this article, we will learn how to disable swap in Linux.


How to Disable Swap in Linux

Here are the steps to disable swap in Linux.


1. View Memory Usage

First step is to get a clear idea of how much swap space is used in your system. You can do this by running the following command.

$ free -h

You will see output like the one shown below. It shows the breakup of your RAM memory and swap space.

In the above output, look for the number under used column for row starting with swap. If it is 0 or nearing 0 it means you are not using much swap space and can safely disable it.


2. Identify Swap partition

Next, run the following command to get ID of all partitions on your system.

$ blkid

You will see the output as shown below. Look for the row with type=”swap” to identify the swap partition and note its UUID.

Next, run lsblk command to identify the [SWAP] partition. The row with [SWAP] at its end indicates the file location of swap partition.

$ lsblk


3. Disable Swap Partition

Run the swapoff command to disable specific swap partition.

$ swapoff <file/path/to/swap>

Here is an example to disable /dev/mapper/centos-swap partition.

$ swapoff /dev/mapper/centos-swap  

If you want to disable all swap partitions on your system, run the following command.

$ swapoff -a

Next, run the free command to check if swap partition has been disabled.

$ free -h

Please note, running the above command will only disable the swap partition until the next reboot. If you want to permanently disable swap space on your system, you need to do the next step.


4. Permanently Disable Swap Space

Open /etc/fstab file in a text editor.

$ vi /etc/fstab

Search for the line starting with the file path to your swap space, as shown below. It may be different for depending on your system but the second column will have the value ‘swap’ in it.

Save and exit the file. Reboot the system to apply changes, or run the mount command to remount all partitions.

$ mount -a

After you have rebooted the system, you can run the following commands to check swap space.

# free -h
# blkid 
# lsblk 

In this article, we have learnt how to disable swap in Linux, both temporarily as well as permanently. You can run these steps on almost every Linux distribution.

Also read:

How to Call C Function in Python
How to Change Default MySQL Data Location
How to Run MySQL Query from Command Line
How to Restore MongoDB dump in Linux & Windows
Script to Keep Your Computer Awake

Leave a Reply

Your email address will not be published.