Macbook Air: how to disable the Swap file (run your laptop faster)

NB: This technique is useful in precisely TWO situations:

  1. EITHER: you have a lot more RAM than you need (on OS X / Apple computers: at least 4Gb, more likely 8Gb), and you want your computer to run faster
  2. OR: you have an expensive SSD hard disk in your laptop, and you want it to last a couple of years or more

Symptoms of a dangerous swap file on the Air

I’ve got a new digital camera, takes very large photos (18 MP). If I load up more than 5 photos in Apple’s Preview, there’s a high chance that Preview *and the whole computer* will seize-up for about 5 seconds. Then it runs normally again.

Preview is a very simple app – it’s not doing anything complex (like, say, Photoshop), it’s just reading images from the disk. If Preview hangs – and hangs the whole OS – there’s something very deeply wrong with your laptop.

There’s no spike in CPU usage – just the OS seems to hang. OS X often does this when it’s waiting for transfer of data to/from somewhere – either the network, or the hard disk. (this is a flaw in OS X’s design – in most cases, the OS shouldn’t hang, but Apple hasn’t coded it that well)

So … I’m fairly sure this is a sign that the SSD drive is being asked to do something it really doesn’t like, and because Apple’s code is imperfect, the OS is being forced – at a very low level – to “hang” while it waits for the hard drive.

Remember: SSD drives are very fast, the drive should have dealt with this in 0.005 seconds, not 5 seconds. That exceptional slowness is what you’re looking for as a sign that something is “wrong”.

Disabling it … permanently

I researched this extensively, and found a lot of interesting stuff about 3 different ways of achieving this. Most of them were from OS X 10.5 (Leopard), where Apple’s swapfile code was a lot weaker. Snow Leopard (10.6) has massive improvements in Apple’s code, and it seems you can get away with this extremely easy technique:

In the Terminal, enter the following command. This will unload the dynamic pager from the Mac OS X kernel:

   sudo launchctl unload -w /System/Library/LaunchDaemons/

Again, this completely disables the Mac OS X paging ability, do not mess around with this for fun.

And then … reboot. I found a couple of useful tips elsewhere on how to check the swap status:

sysctl vm.swapusage

Outputs something like this:

   vm.swapusage: total = 0.00M  used = 0.00M  free = 0.00M

Re-enabling it…

In the Terminal, enter the following command:

   sudo launchctl load -w /System/Library/LaunchDaemons/

Why / (not) disable the swap file?

Actually, I didn’t really want to – I have a 1st-gen Air, and it only has 2GB Ram. With OS X, that’s *not enough* to forego a swap file (NB: Windows applications tend to be slightly less memory hungry, and linux apps a LOT less mem hungry. With Linux, 2 Gb is plenty, with Windows it’s “probably” OK)

BUT … when my Air’s hard disk got fried, I replaced it with an SSD drive. The change is enormous – the Air runs literally 2-3 times as fast on many operations (basically: if you buy an Air without SSD, you are wasting your money, and you’re getting ripped-off)

However … SSD drives are *destroyed* by swap files (literally – the hardware gets worn down and soon breaks, permanently). Interestingly, MS Windows will automatically disable lots of its own behaviour to reduce the chances of this happening. As does Linux. Apple (as ever) appears to be behind the times: allegedly, OS X doesn’t do this (even though Apple sells Air’s with SSD drives!) … but then again, no-one seems to know for sure.

Ultimately, the bizarre OS hangs scared me into doing this – I know that the cheap SSD drives have very short lifespans if you don’t use them properly – measured in months, rather than decades. And by “cheap” I mean “anything costing less than $5,000”. That definitely includes anything I could afford, then ;).

7 Replies to “Macbook Air: how to disable the Swap file (run your laptop faster)”

  1. This is exactly why I’ve gone for the 4GB RAM option on a new MacBook Air. I’ll have to monitor how it goes and see if I want to turn off the swap file. Do you see any out-of-memory errors when you do this, BTW?

    Also I’d just point out that all new MacBook Airs are sold with SSDs which is completely sane, given the price of SSDs these days and the advantages (not least in shock/vibration tolerance) over HDD.

  2. @Will – I’ve *so far* not seen those sudden intense slowdowns / OS hangs.

    However … I’ve had a spate of over-heating problems instead. This could be co-incidence, but I have vague memories that OS X makes quite a lot of “assumptions” about how the computer “ought” to be used, and then hard-codes them. IIRC, in the past some of these have mixed badly with the BSD/unix core (indeed, swapfiles used to be a good example – Apple seemed to pre-emptively swap everything, always, even when there was no good reason for doing so).

  3. Overall speed is noticeably a little faster (which is a shock). The improvement *feels like* 20% faster again on top of the improvement from switching to SSD.

    HOWEVER … memory-hungry apps seem to run about 50% to 25% of their normal speed – but only once they start gobbling memory.

    e.g. Firefox with 2 windows and 10 tabs – fine, slightly faster than normal (so small could be imaginged). Firefox with 6 windows and 50+ tabs – runs 2x slower than normal.

    NB: the OS *does not* hang with these slowdowns, just the individual applications run slower.

    I thought that OS X didn’t do this any more, but … from the above observations, I wonder whether OS X still has some hard-coding internally, and it panics if it can’t swap – even when it doesn’t need to! Result (perhaps) – a kernel process / daemon / semaphore / queue is doing a busy-wait, “hoping” that some swap-space will appear.

  4. @Mark – No out-of-memory errors. However, each time an app has slowed-down, I’ve killed other apps that I wasn’t much using, “just in case”.

    I’m usually running at about 200Mb “free” if using only 1 big app and several small ones, or 50Mb “free” if using multiple big/medium apps.

    e.g. right now with: Firefox (huge), Skype (medium), Texedit (small), I’ve got:

    Wired: 700Mb
    Active: 300Mb
    Inactive: 1,000Mb
    Free: 50Mb

    …and no, I can’t remember WTF “Inactive” means – I remember researching it before, and finding multiple conflicting definitions ;).

    NB: despite having “50Mb free”, the laptop is running fast and cold right now.

  5. @Mark

    One more thing – if I had 4Gb in an Air, and an SSD, I would *definitely* turn off the swap. I’ve been running my Air for years with 2Gb RAM + 2Gb max swap, and never run out of memory.

    I vaguely remember that when I was running 3 copies of Eclipse *and* Photoshop Elements (*and* all my normal apps: Firefox with 100 tabs, etc), my Swap went over 2Gb. But I might have imagined that. Certainly, I can run 2-3 copies of Eclipse in parallel within 2Gb swap.

    i.e. I can’t imagine anything you would do on an Air that would EVER need more than 4Gb memory. All the things I can think of – e.g. Video editing – would fail anyway on the Air due to the (relatively) slow processor and/or heating problems of running at 100% CPU for protracted periods.

    Although, also … if I bought a new Air, I’d work really hard to find an official tech guy at Apple to get an opinion on the Swap, since I’d be planning to keep it turned-off forever. Perhaps register as a Mac Developer ($99/year) and use one of your free annual tech-support requests to get an answer from the OS team? :)

  6. You get can away with 2GB on Vista/7, but only if you’re using readyboost, ime.
    Otherwise I strongly recommend 4GB.

    Firefox’s problem is every addon uses memory for every tab. (Yes, you MAY wince now)

Leave a Reply

Your email address will not be published. Required fields are marked *