How can I turn off swap permanently?

Swap makes my system all sluggish and turning it off makes everything smoother. I have 3.5 GB of RAM.

I know how to turn swap off by entering sudo swapoff -a in the terminal, but that's just for the current session, because after a reboot swap turns itself back on. Is there any way to turn swap off permanently?


Solution 1:

May I suggest a safer approach? You never know when swap can save you from crashing. While swapping does indeed slow down your computer, if you use a program that eats up all your RAM, having the ability to swap can save you from a hard reboot. When the computer starts swapping, you'll notice and be able to kill the offending application.

So, instead of disabling swap altogether, just make sure your OS swaps very rarely. This is controlled by the vm.swappiness setting in /etc/sysctl.conf. Ubuntu's default setting is 60 if I remember correctly which is too much for most situations and will cause you to start swapping even while RAM is still available. If you reduce this value, you will be able to keep the safety line of swap while only using it for emergencies. So, open the file:

sudo nano /etc/sysctl.conf

And add this line to it:

vm.swappiness=10

If that is still too much, change the 10 to 1. Now, after you restart, you will only swap when absolutely necessary and you can simply forget about it.

Solution 2:

Find the line in /etc/fstab referring to swap, and comment it. Mine is like this:

UUID=6880a28d-a9dc-4bfb-ba47-0876b50e96b3 /               ext4    errors=remount-ro 0       1
UUID=7350e6f2-e3a7-4d80-9a95-8741c7db118f /home           ext4    defaults        0       2
UUID=E2E26AD1E26AAA0D /media/windows  ntfs    defaults,umask=007,gid=46 0       0

# Swap a usb extern (3.7 GB):
#/dev/sdb1 none swap sw 0 0

You can edit this file with gedit. Backup it first, just in case:

sudo cp /etc/fstab /etc/fstab_backup
gksu gedit /etc/fstab

Just add # to the beginning of the line where swap is, and reboot the computer.

OR

Try Command-line way of commenting out swap entry in /etc/fstab file,

sudo sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

Solution 3:

Wrong.. if systemd finds a SWAP partition on any drive it will anyway automount it. Strange that nobody mentioned systemd. The solution is quite simple anyways; to permanently disable swap you must:

  1. swapoff -a
  2. edit /etc/fstab and comment any swap entries if present (you might be able to skip this step 2, and step 3 without step 2 may work for you).
  3. run: sudo systemctl mask "dev-sdXX.swap" (where XX is the swap partition. Note, you should use quotes around the unit name in case the name has escape characters/backslash. Also useful to do it for all possible partitions so that if there is a swap partition on any other drive it will not be mounted.)

Peace out.