Linux Commands

How to Fully Display the Swap Space on Linux

Swap space is the virtual memory to expand your system’s RAM capacity. Hence, when RAM is unavailable, you can use Swap for RAM by moving the inactive memory pages to the swap area. However, for the spinning speed of a classical hard disk, the swap space is lower in transfer access time and speed compared to RAM.

Swap space can swap out the RAM contents in case there is not enough RAM available. This swap space, representing a physical memory page, uses a special disk file or the top of a disk partition when the RAM is full. Swap space is optional if there is enough memory in the system. In that case, you can disable the Swap space. This tutorial looks at how to fully disable the Swap space on Linux.

How to Fully Disable the Swap Space on Linux

First, check if the Swap is on or off, and run the “swapon –s” from the shell interface. If the Swap is enabled, the command returns a non-empty output:

sudo swapon -s

Or

sudo swapon --show

Before fully disabling the Swap space, you must check the occupied memory. Through this, you can identify the partition which holds the Swap space in your system. You can use any of the following to check the Swap space:

free -h

Or

free -mh

As you can see, the Swap space for the system is 2GB. Now, we need to fully disable the Swap space. To get more reliable information from the previous command, you can get the “/etc/fstab” file by opening it in any editor. Remember, you must open the file in the editor to completely disable the Swap space on Linux. Here, we open this file using the nano editor.

sudo nano /etc/fstab

Now, convert the entire line to a comment by prepending it with the “#” (hashtag) symbol.

Save the file. Now, you can check your system’s Swap space through the free command:

free -h

You can see that the Swap space is free.

By running the “swapon –s” command, you will see that it no longer gives you any output, which means that the Swap space is fully disabled on Linux.

swapon -s

This is how you can fully disable the Swap space on Linux. If you want to disable a specific Swap partition, run the following “swapoff” command:

swapoff <file/path/to/swap>

Use the following command to disable all Swaps temporarily:

swapoff -a

Conclusion

Swap memory is a part of the system’s hard drive or other storage devices which is kept separately. Swapping (enabling swap space) greatly reduces the performance of your system. So, if your system has enough RAM, you can disable the Swap space. This tutorial explained how to fully disable the Swap space on Linux.

When there is a limited amount of RAM, disabling the Swap space can also have unintended consequences such as running out of memory. Disabling a Swap without any specific reason is not recommended. It is always slow compared to RAM, which is why using Swap space negatively affects the system performance.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.