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?

Alex Li
  • 1,291
  • 3
    AFAIK the swap is used for hibernation. Just sayin' – DanMan Mar 28 '14 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. – animaletdesequia Mar 29 '14 at 15:07
  • Simply put swapoff -a in /etc/rc.local and forget about that... ;) – user264467 Apr 02 '14 at 06: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 '16 at 07:42

6 Answers6

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.

terdon
  • 100,812
  • I love changing swappines, and I've not done in years. Thanks for the reminder. sudo upvote turdon! – Darth Egregious Mar 28 '14 at 17:18
  • 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 '14 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 '14 at 23:41
  • What if I change swap to 0? How would that turn out? 0 means it won't use swap unless absolutely necessary right? – Alex Li Mar 29 '14 at 19:31
  • @AlexLi no, 10 already means that. With 10 you will see next to no swapping. Setting it to 0 is not a good idea, seriously, swapping can really save you at times and setting a low swappiness value will solve your issue without making the system unstable. I have not tested this, but presumably, setting to 0 will disable swapping and bring all the problems associated with a lack of swap. – terdon Mar 29 '14 at 19:33
  • 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 '14 at 22:19
  • @jmiserez 1) my answer was posted after the two answers here that give a way to ignore the swap partition, that's why I started it with "may I offer an alternative?". 2) Actually, a few gigs of swap can make all the difference in the world, the system usually gets noticeably slower when swapping and one or 2 gigs may well give you enough time to kill the offending process and not deal with the OOM. 3) None of these approaches get your swap partition back so your point about HDD space is irrelevant. 4) I didn't even mention hibernation. – terdon Mar 30 '14 at 18:07
  • & 2) Fair enough. 3) It's easy to reclaim the space using gparted, once swap is disabled. 4) On Ubuntu, hibernation uses the swap partition, so the two share a common fate. 5) FYI: You can also use the zram-config package to enable memory compression once RAM runs out. That should also give you some breathing room runaway processes.
  • – jmiserez Mar 31 '14 at 05:38
  • Since I have put a 512GB SSD (almost 3 years ago) into my notebook, I have set it up without any Swap partition. First I had 8GB of RAM, now I have 16GB. I actually never had a problem plus I do not want to waste my SSD space with SWAP and hibernation... – Consumology Apr 02 '14 at 11:47
  • 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 '14 at 17:35
  • @BCS From my experience, the system usually will start "sucking mud" when there is not enough or no swap space available; what happens is, the system will get stuck in a continuous loop paging the hard drive for available swap space that does not exist and so will never be found. I've fixed this problem in the past by returning swappiness back to 60 and increasing the size of the swap partition; you see, "sucking mud" is exactly what swappiness prevents. – mchid May 03 '15 at 02:23
  • 1
    @mchid In most of the cases where I've had this problem, it was caused by a massive over commitment (e.g. a bug causes my project to allocate an use few dozen GB more than I have free). While in theory this might eventual work through, it takes so long that I am unwilling to wait and am forced to reboot the system. What I want is a solution that will have a chance of recovering with out the reboot. Practically, this requires OMM killing the correct offender. – BCS May 12 '15 at 22:50
  • 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 02 '16 at 09:07
  • 2
    @mchid My "solution" to the problem is turning off swap. Personally, I'd rather have processes crashing on me than a completely locked system. "if your system is swapping, you shouldn't have any problem", that's my point: I do have problems and lots of them. ZRAM was a nice idea back in the day, but in practice it turns out to do more harm than good.

    And "run the command before you run into trouble" requires me to constantly keep an eye on system memory. That's not how I or most people use a computer, and they shouldn't. The device serves me, not the other way around.

    – fat-lobyte Sep 13 '16 at 10:08
  • 1
    @mchid, I absolute agree with fat-lobyte, unfortunately. I have a fast SSD for swapping space, but it still takes ages and is unusable; this happens due to my faulty code. I prefer strongly to kill the system and reboot as well. We're both not offering a better solution, however, the killing is a better solution then swapping; the latter takes 30 minutes on my machine as well (until OOM killing processes are invoked). It's a nightmare. – Mayou36 Nov 02 '20 at 20:02
  • 1
    @mchid please stop having long conversations in the comments. Take it to chat or anywhere else that doesn't ping me. – terdon Nov 03 '20 at 08:54