Do you really need to disable swap on your Linux system permanently? In the Linux operating system, there is this feature called swapping. What it does is it expands the system’s RAM as physical memory gets full by reserving a small partition on the hard disk or by using a special disk file.
Access time speed significantly increases when using SSD for swapping compared to the conventional HDD. But if you compare RAM and SSD, RAM speed is still considerably higher.
Swap: The Basics
There are two reasons why swapping is essential. First, when no physical memory is available, and the system requires more. Less used pages get swapped out by the kernel, and processes or applications that need memory will immediately get provisioned.
Second, during the startup phase, applications may use only a certain number of pages for initialization. Since these pages will not be used again, the system can swap out these pages to free up memory for other applications.
Swapping is a great feature. However, it does have its pros and cons. We already know that disks are very slow compared to RAM. Disk speeds are measured in milliseconds, while memory speeds are measured in nanoseconds. Accessing disks compared to physical memory can be tens of thousands of times slower.
There are times when excessive swapping or thrashing occurs. In such situations, the system struggles to find free memory, and with this, the only solution would be to add more RAM. This may happen if you have swap turned on, but if you have it turned off, chances are your system will crash entirely.
You may want to reconsider disabling swaps on your Linux system if you do not have sufficient physical memory or RAM. The rule of thumb, many years ago, regarding the amount of swap space to be allocated, should be twice the amount of the machine’s physical RAM. Nowadays, RAM is no longer measured in KB or MB.
RAM has become so cheap that regular computers these days have at least 8GB, and some even extend up to 64GB. If you have this much RAM in your machine, then it’s possible that your swap space may never be used, especially if you don’t have that many processes running.
To disable swap permanently on Linux, you will need a sudo privileged user account and some basic knowledge in
How to Permanently Disable Swap on Linux?
Verify Swap Space and or Swap File
The first thing we would need to do to disable swap on Linux permanently is to confirm that the swap partition or swap file exists and that it is enabled. Second, we need to ensure that swap is not in use.
$ free -mh
Here we could see that the total size for swap is 2GB, used is zero, and free or available swap space is 2GB. In this screenshot, we have confirmed that swap is not being used.
Here you would need to check for the Type=”swap”.
In this screenshot, we confirmed that the [SWAP] partition is present and mounted.
$ vi /etc/fstab
We will need to run this command next to verify if a swap file has been configured on your Linux system. The screenshot below is what you would see after entering the command.
In this screenshot, we have confirmed that we have a swap partition, but no swap file has been configured.
Turn Off Swap / Unmount Swap
Before we permanently disable swap on your Linux system, we have to turn it off first. Since we already have confirmed that swap is not in use, we can now deactivate swap. Run the command below to deactivate swap. For this command, we will need a sudo privileged user account.
Enter your sudo privileged user account password when prompted.
$ sudo swapoff -a
As you can see, there is no indication that the command was executed successfully. We need to run the commands again below to verify if swap has indeed been turned off.
In the screenshot above, you can see that the total size for the swap partition is now zero, and the swap partition is no longer mounted.
$ free -h
Here, we could see that there is no longer any entry for Type=”swap”.
Disable Swap Permanently on Linux
Now that we have turned off or deactivated the swap partition and verified that there are no existing swap files. We can now edit the /etc/fstab file to make sure that swap is disabled permanently on our Linux operating system.
Edit /etc/fstab File Using vi
Run the command below with sudo included to edit /etc/fstab file and be able to save changes.
$ sudo vi /etc/fstab
We need to comment out the entire line for the swap to disable it permanently even after rebooting your Linux operating system. If you skip this step, the swap partition will automatically be re-mounted and reactivated after a reboot.
How to Navigate When Inside vi
The screenshot below is what you will see after entering the command above. To navigate inside
vi, you must use the arrow keys to move the cursor. To be able to make changes, you will need to press on the letter “i”.
This will turn on the insert operation, and at the bottom of the Terminal, you should see –INSERT– as an indication that insert mode is on.
Like what you see in the screenshot below, you will need to place a “#” character at the beginning of the swap line to comment it out. This will make the system read the entire line as a comment instead of a system configuration.
Saving Changes in vi
To save the changes you made, press on the “Esc” key several times to make sure you are out of the insert-operation mode. Enter the character “:” to be able to enter a command like “
x” to save all changes and exit. You can also type “:w” and then hit enter to save the changes you made without exiting from
You should see an indication at the bottom of the screen that says “written”, like what we have in the screenshot below. This means that changes have been saved.
And that’s about it for this tutorial. We looked at what the swap feature on Linux is all about, and how to verify if swap is present and enabled on your Linux system. We also showed you how to disable swap on Linux permanently. And lastly, we also learned the basics of using the vi command and how to navigate in it.
If this guide helped you, please share it. ?