How to disable swap on Linux

Swap is virtual memory mechanism that can provide additional system operating memory. When the system needs to free up some memory for applications, it temporarily moves data from the RAM to a storage device. Swaping is important for the operating system, but as storage drives are slower than the RAM memory, this process can sometime slow down a system. That’s why sometimes removing swap is neccessary.

Warning: Keep in mind that disabling swap can affect system stability. In case there is not enough operating memory, Linux will usually kill processes which can lead to unpredictable behavior.

In this article we’ll go through the process of disabling the swap memory and eventualy re-enabling it.

Disabling swap

Linux swap is essentialy a special partition on the storage drive

To disable swap you can use the following command:

swapoff -a

This will remove active swaps, but swap will be reestablished on next system reboot.

To disable swap creation on boot you should edit the /etc/fstab configuration. Swap is created as a special partition. To stop it comment out any ‘swap’ lines in the /etc/fstab.

An example for the swap mounting line from Ubuntu’s /etc/fstab:

/dev/mapper/cloud--vg-swap_1 none swap sw 0 0

Comment it out by adding # in the beginning of the line.

Re-enabling swap

To enable swap again you’ll need to make sure that there is a swap partition available. The lsblk can provide you a hint on the available partiotions.

If you have previosly disabled the swap, re-enabling usually requires to simply run:

swapon /dev/mapper/cloud–vg-swap_1

Where /dev/mapper/cloud–vg-swap_1 is the swap partition.

Don’t forget to uncomment the lines in the /etc/fstab aswell.