109

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?

4
  • 3
    AFAIK the swap is used for hibernation. Just sayin'
    – DanMan
    Mar 28, 2014 at 21:50
  • 2
    Yes it is, but it still can be used. I have a little script that mounts the swap partition when the system goes into hibernation and un-mounts it when it comes back. The swap file doesn't need to be active in the meantime. Mar 29, 2014 at 15:07
  • Simply put swapoff -a in /etc/rc.local and forget about that... ;)
    – user264467
    Apr 2, 2014 at 6:00
  • 2
    @user264467: it is quite counterintuitive to leave swap enabled just to disable it on every reboot, it is much cleaner to disable it permamently.
    – karatedog
    Dec 12, 2016 at 7:42

6 Answers 6

145

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.

16
  • 23
    Just to add to the 'safer approach' argument: When you have no swap and the system runs out of memory, the Linux OOM (Out Of Memory) killer is invoked. This selects a process based on a 'badness' score and then... kills it (terminates, closes) with no chance for the application to save its changes, clean up, etc. Thus, if you turn off swap and run out of memory, you very well run the risk of losing work if important processes score poorly in the OOM killer's eyes. So, you should only turn swap off if you are confident that you will never run out of memory.
    – Reid
    Mar 28, 2014 at 21:21
  • 7
    @Reid worth pointing out that, obviously, the same exact thing will happen if you run out of swap space too.
    – o0'.
    Mar 28, 2014 at 23:41
  • 17
    This does not actually answer the question. Disabling swap altogether can make a lot of sense on a system with 16GB or 32GB of RAM. A few extra GB of swap will not do any good in the case of memory-eating applications, while a large amount of swap (e.g. 20GB) would take up a lot of harddrive/SSD space. Also, hibernating with 32GB of RAM is not fun.
    – jmiserez
    Mar 29, 2014 at 22:19
  • 12
    For some setup, as soon as you run out of physical RAM and starts swapping, the system starts "sucking mud" and becomes unusable. Yes, in theory you can get in and kill the offender, but when it takes 20+ minutes to change focus in the UI and even longer to get a password prompt via SSH, the OOMing ends up being the best option (followed by a hard reboot, followed by getting in and fixing the problem).
    – BCS
    Aug 12, 2014 at 17:35
  • 2
    @mchid Your suggested solutions aren't feasible when the UI is so frozen up that changing Windows takes 5 minutes, and waiting for command execution takes 10 minutes. It takes long enough if you know exactly what you are doing, but if you have to run several commands to find the worst memory offender, you can multiply that time by 3. For example, your drop_caches command creates 3 processes (echo, sudo, tee), which can take ages when your system is swapping. I am not kidding, btw. My swap is on a slow hard drive and recovering from OOM without a hard reboot takes about an hour.
    – fat-lobyte
    Sep 2, 2016 at 9:07
57

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
2
  • I get this when I type gksu gedit /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> /host/ubuntu/disks/root.disk / ext4 loop,errors=remount-ro 0 1 /host/ubuntu/disks/swap.disk none swap loop,sw 0 0
    – Alex Li
    Mar 28, 2014 at 14:27
  • 1
    So, do what he's saying. Comment out the first line that refers to "swap". :P
    – cHao
    Mar 29, 2014 at 2:42
41

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.

4
  • 3
    number 3 for the win!!
    – oneklc
    Oct 9, 2019 at 21:55
  • 1
    Number 3 is essential for Ubuntu 18.04 or maybe because of the transition from 16.04->18.04 and/or maybe because of the installation on nvme.
    – vigilian
    Nov 5, 2019 at 9:28
  • Do we have the opposite command just to know ?
    – vigilian
    Nov 5, 2019 at 9:29
  • 3
    sudo systemctl unmask dev-sdXX.swap
    – tgwaste
    Jan 22, 2020 at 2:44
18

Just delete the entry (or comment it using # in front of the line) from fstab and reboot. Look for the line with the word "swap" in it.

sudo nano /etc/fstab
2
  • 12
    This one would work too, but I think is better commenting the line rather than removing it completely, just in case you want to revert the changes. Mar 28, 2014 at 14:11
  • 1
    I have edited my post.
    – mdalacu
    Mar 29, 2014 at 9:31
6

Complementing Zibri's answer:

  • swapoff -a will disable all swaps temporarily.
  • If you have some swaps in /etc/fstab, just comment them out and reboot.
  • In my case (OdroidMC1+Armbian), the swap is controlled by systemctl. To find the responsible, use systemctl --type swap.

Example:

# systemctl --type swap

UNIT           LOAD   ACTIVE SUB    DESCRIPTION                                                                                                                                                                     
dev-zram1.swap loaded active active /dev/zram1                                                                                                                                                                      

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
  • Disable by masking it with sysctl:
# systemctl mask dev-zram1.swap

Created symlink /etc/systemd/system/dev-zram1.swap → /dev/null.
1
  • Thanks for your contribution! Please note that because we use a Q&A format, you can't reply to other answers or comments. It's best to try write your answer in a way that doesn't require referencing another answer since existing answers can be modified. It can also be confusing to follow two answers that might be in conflict with one another. If you need to reference other content, make sure that you use a permalink since usernames can also change over time.
    – Nmath
    Nov 22, 2020 at 6:02
3

Disable Swap Permanently

swapoff -a                 # Disable all devices marked as swap in /etc/fstab
sed -e '/swap/ s/^#*/#/' -i /etc/fstab   # Comment the correct mounting point
systemctl mask swap.target               # Completely disabled

Note: All Commands Should Run with Root Privileges

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .