In this quick guide, we will be showing you how to disable the swap space on Ubuntu.
Swap space is used by the Ubuntu operating system to temporarily store data on your disk that generally would be put into memory. This space is only ever used when your system is low on memory and helps save the system from out-of-memory errors.
While it sounds good in theory, swap memory is significantly slower than RAM. Being slow can substantially reduce the performance of your applications every time it has to write to memory.
If you would rather Ubuntu just kill applications when it runs out of memory, then you can turn off the swap space.
Over the following guides, you will learn how to turn off the swap functionality on your system.
Turning Off Swap on Ubuntu
The next few sections will cover checking to see if swap is enabled and then permanently turning the feature off.
These steps are relatively straightforward to follow and can be reversed with a few simple commands.
Checking if Swap is Enabled on Ubuntu
Before you turn off the swap on your Ubuntu system, you should first check to see whether it was enabled in the first place.
1. The easiest way to check if swap is enabled on your system is to utilize the following command.
By using the “--show
” option, we are telling the “swapon
” tool to print out the swap spaces on your Ubuntu system.
swapon --show
2. If you have swap enabled on your device, you will see something similar to what we have shown below.
NAME TYPE SIZE USED PRIO
/swapfile file 1.8G 1M -2
If there was no text output, you do not currently have swap enabled on your system, and there is no need to continue with this guide.
Turning off Swap on Ubuntu
3. To turn off swap on the Ubuntu operating system, you only need to use the command below.
This command will immediately tell the system to stop using the swap space. By using the “-a
” option, we are telling it to delete all swaps.
sudo swapoff -a
4. Even though we turned the swap functionality off, it will be restored when you restart your system.
To permanently turn off swap memory on Ubuntu, you must modify your fstab file. The fstab file is what your system uses when mounting disks.
You can begin editing this file by using the following command.
sudo nano /etc/fstab
5. Within this file, find the following line. Sometimes “/swapfile
” will be written as "/swap
” or “/swap.img
“.
/swapfile none swap sw 0 0
6. Once you have found the line, add a hashtag to the front of it to comment it out. It should look like what we have shown below.
#/swapfile none swap sw 0 0
7. With this change to your stab file, save and quit by pressing CTRL + X, Y, then the ENTER key.
8. To test that everything works, you should try restarting your Ubuntu system.
Restarting lets you verify that the swap space won’t re-enable when your system starts back up.
sudo reboot
Verifying that Swap is Disabled
9. Once your system has restarted, you can verify that you have turned the swap off on your Ubuntu device by using the command from the first section.
swapon --show
If nothing appears, then you have successfully turned off the swap memory on your device.
Conclusion
Hopefully, at this point, you will now understand how to turn off the swap memory on your Ubuntu device.
Turning off the swap memory makes your system entirely reliant on its physical RAM.
Please comment below if you have any questions about turning off the swap on your system.
If you found this tutorial useful, be sure to check out our many other Ubuntu guides.