Disabling swap file

I have an imac from 2009. At the time, I maxed out the memory to a whopping 4GB. This may not seem much these days but it is still plenty for most things.

Over time, my disk has gotten a bit fragmented, applications have gotten a bit heavier (e.g. Firefox commonly uses well over a GB), and things have gotten a little slower.

I had already half decided to buy a new mac to ‘solve’ this problem by simply getting the latest model and maxing out the RAM. The only problem is that Apple seems to be dragging their heels refreshing their iMac line, which is now more than a year old (ancient basically). So, I’ll have to survive for a bit and figure out how to do that until they get around to fixing that.

Basically the primary reason for slowness on macs is swapping. You may think you have enough RAM, and you may in fact have plenty of available RAM. It doesn’t matter, your mac will swap applications to disk even when you don’t need the memory. That takes time. Especially on fragmented disks. Basically on my machine it got to a point where the disk would be churning for minutes every time I opened applications and there was considerable lag when switching applications that I had already opened. All this despite still having a full GB of memory available. So even though I am nowhere close to running out of memory, the OS decides to spend time swapping stuff to and from disk almost constantly. Reason: it was designed well over a decade ago during a time when 16MB was massive amount of memory. I have about 250x that amount.

I found this nice post explaining how to turn swapping off (you have to reboot for this to work). Now there are all sorts of reasons why swapping and virtual memory are good things in theory and all sorts of advice warning that all sorts of evil things can and will happen if you mess with it.

Consider yourself warned.

There are also people claiming that swapping “improves performance”. The latter point is of course massive bull shit: how does using a massively slower disk help make things faster compared to only using RAM, which has massively better bandwidth and latency?. There are also loads of people confusing virtual memory and swap space whining that applications won’t run without virtual memory. Virtual memory is the notion that applications have access to a memory space that is much larger than the available RAM. On a 64 bit machine, this typically is way more than the capacity of your disk or indeed most storage solutions (think a couple of hundred TB).

Swap space on the other hand is used for temporarily swapping memory to disk that has actually been allocated and is in use by some application to free it up for some other application. I.e. you don’t swap virtual memory but only memory that is actually used. So, swapping is only useful when you need more RAM than you have and when you somehow prefer to not close applications and instead have their memory written to disk and read from disk when you switch between them.

Provided you have enough RAM to run all the applications that you need, this should be exactly never. On servers this is the reason you generally turn swapping off because by the time you need it, your server is dead anyway. On desktops, the same argument can be used, provided you can fit your applications into memory comfortably.

So, I turned off swapping on my mac a few days ago and was rewarded with a massive reduction in disk activity and nearly full restoration of the performance I enjoyed when this machine was still new and 4GB was four times what you actually needed. I call that a huge win.

Now, there is one caveat: modern operating systems run hundreds of processes. Those processes use memory. Nasty things happen if it cannot be allocated. The price of running out of memory is that random processes may crash and fail. If that happens to a critical process, your entire OS becomes unstable and you may have to reboot. Solution: make sure you don’t run out of memory. The good news is that normally you shouldn’t run out of memory except when running buggy software with memory leaks or when running a lot of software or software with extreme memory needs (e.g. Adobe Aperture or some games). A little awareness about what is running goes a long way to avoiding any trouble.

I routinely have Firefox open with a few dozen tabs, spotify, skype, a terminal, eclipse, and maybe preview and a few other small things. With all that open, I’m still below 3GB. That leaves about 1 GB for file caches, misc other OS needs, and a little bandwidth to open extra applications. If I run low on memory, or suspect that I will, I simply close a few applications.

I used to do this with windows XP as well and only recall a hand full of cases where applications crashed because they ran out of memory. Well worth the price for a vast increase in performance. Turning off swapping on old PCs is pretty much a no brainer and especially on new ones with way more memory than you need (and considering memory prices, why would you run with less …), I cannot possibly imagine any scenario where swapping adds any value whatsoever but I guess it would be a nice fallback solution as a last resort when the only alternative would be for applications to crash. Unfortunately, neither macs nor windows pcs have a setting that would enable that. So, turning off swapping entirely is the next best thing.

4 Replies to “Disabling swap file”

  1. Disabling swap will always reduce performance of the whole system IF the OS is working correctly. The simple reason is that even though all your applications could fit the memory, the changes are very slim that all your applications AND all your file system fits the memory. If you disable swap and all your applications take close the whole memory, all your disk access is practically the same as swapping stuff in. The only difference is if the OS is swapping the application memory in or reading file data from the HDD. Either way, you’re waiting for the HDD.

    Of course, if your OS is insane or cannot be tweaked for your workload (e.g. /proc/sys/vm/swappiness and /proc/sys/vm/vfs_cache_pressure settings in Linux), it might make sense to disable swapping. Even in that case, it could make sense to allow swapping but limit it to pretty small value (around 100-200 MB). In practise, that buys you extra 100-200 MB of disk cache. One way to decide the size of the swap is to consider the minimum read bandwidth for your disk and then multiply that by the number of seconds you’re ready to wait for the swapping to complete in worst case scenario.

    I’m writing this on a Ubuntu workstation having 8 GB RAM and I’m still running 700 MB on the swap and the kernel is using currently 540 MB for the disk cache. If I didn’t have swap enabled, my effective disk cache would be around -160MB! In the same time, my disks are getting around a single transaction per second so I’m not constantly swapping in or out.

    1. In theory yes, with some systems. In practice, both windows and osx are configured such that if swapping becomes an issue, you are better off without it. The problem is that when it becomes an issue, it really sucks the life out of the system if the OS is constantly accessing the disk, especially if it is a slow disk. Swapping out a few hundred MB on a laptop disk takes an amount of time that is measured in seconds. That means whatever application owned that memory will have some serious lag when you switch back to it. Also it means that if any of your other applications need to access disk for valid reasons, those now get even less IO bandwidth.

      Such systems become unusable anyway if the amount of RAM crosses that of what is physically available. So if you have 4GB and you applications use 6GB, just about anything you do will involve moving around GB of data on a very slow overworked disk. Solution: don’t use up 6GB and close some applications.

      The only time when swapping makes sense is when you have plenty of memory and the swapping operations take milli seconds rather than seconds. When that’s no longer the case it becomes a problem.

      This is what has been happening on OS X on my imac recently. I have 4GB and typically have a browser, spotify, a handful of smaller apps open. If you add up the memory they use, it is generally hovering between 2 and 3GB. With swap turned off, I can alt tab between those applications without lag. With swap turned on, I face long delays when switching applications and stuttering video when the OS decides to dedicate it self to moving bits around on the disk instead of playing video.

  2. Apparently Apple must have thought I was right given their tweaks in Mavericks. I’ve upgraded my old imac, which is now running much smoother. After running it for several days, it has allocated only 5MB for swapping and it has several hundreds of MB of compressed space that in earlier versions of OS-X would have translated in more than a GB of swap. It not accessing the disk at all makes a huge difference.

  3. Just found your entry, and I wholeheartedly recommend it. I’m always running without a swap file (8 GB of RAM, and my machine swapped frequently before), and I never ran into a problem. Games, Aperture, FCPX – all went well. Only when I really come to the border of my RAM, the machine freezes for about 3-5 seconds, where it’s allocating the unused RAM files to the HDD. Then again, smoothness ensues. So, I can recommend disabling swap for every user out there. It does so much for your PC or Mac, even if you can’t or don’t want to buy a SSD.

Leave a Reply