Swap in Linux is a space on a disk drive (HDD or SSD) that is used when the amount of physical memory (RAM) tends to get full.
Although it protects Linux system from getting out of memory, in certain cases it is recommended to disable swap.
For example swap should be disabled on Kubernetes nodes, on database servers where I/O performance is critical and on SSD drives to increase their lifespan.
In this note i will show how to disable swap permanently on Ubuntu, though these commands should work for the other Linux systems as well.
Cool Tip: How to change swappiness (swap usage tendancy)! Read more →
Disable Swap Permanently
List all swap devices and files:
$ cat /proc/swaps Filename Type Size Used Priority /dev/dm-1 partition 1003516 129996 -2
Turn off swap immediately:
$ sudo swapoff -a
At this point swap can be re-enabled with:
$ sudo swapon -a
To disable swap permanently, comment out (or remove) any swap entries from /etc/fstab
:
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Finally, you can delete the actual swap files using the rm
command, for example:
$ sudo rm /dev/dm-1
If the swap space was created using LVM, you may also want to remove the corresponding logical volume:
$ sudo lvscan ACTIVE '/dev/ubuntu-vg/root' [114,52 GiB] inherit ACTIVE '/dev/ubuntu-vg/swap_1' [980,00 MiB] inherit $ sudo lvremove /dev/ubuntu-vg/swap_1
Thank you so much for sharing this information.
Best regards.