Dev ❤ Ops

How to disable swap space Linux

Swap space, also known as swap memory or swap partition, is a portion of a computer’s hard drive or other storage device that is set aside for use as virtual memory. Virtual memory is a feature of an operating system (OS) that enables a computer to be able to compensate for shortages of physical memory by temporarily transferring pages of data from random access memory (RAM) to disk storage.

Swap space is used when the amount of physical memory (RAM) is full and the system needs additional memory. When this happens, the system can temporarily transfer some data from RAM to the swap space, freeing up physical memory for other processes. The system can then access the data stored in the swap space when needed, just as it would access data in RAM.

Swap space is typically used as a last resort when the system has exhausted all other options for obtaining additional memory. It is generally slower than RAM, so using swap space can negatively impact system performance. However, it can be useful in cases where the system needs more memory than is available in physical RAM, or when running multiple programs that require a lot of memory.

Disable swap space:

To disable swap on Ubuntu, you can follow these steps:

Step 1. Open a terminal

Run the following command to view the current swap space usage.

sudo swapon --show
Step 2. Disable Swap

If there is any swap space being used, you can disable it by running the following command.

sudo swapoff -a
Step 3. Permanently disable swap space

To permanently disable swap space, you will need to edit the /etc/fstab file. Run the following command to open the file in a text editor.

sudo vi /etc/fstab

Find the line that refers to swap space and add a # symbol at the beginning of the line to comment it out. This will prevent Linux from attempting to mount the swap partition or file when it boots up.

root@devops:~# cat /etc/fstab  | grep swap
#/swap.img	none	swap	sw	0	0
Step 4. Save the file and exit the text editor
Step 5. Verify Swap

Run the following command to verify that swap is disabled.

sudo swapon --show

You should see that no swap space is being used.

Note: Disabling swap may have unintended consequences, such as causing the system to run out of memory if you have a limited amount of RAM. It is generally not recommended to disable swap unless you have a specific reason to do so.

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.

Follow us on LinkedIn for updates!

Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “How to disable swap space Linux”