Hi, I'm Adam. Email me about: CTOs, Mobile/iOS, Project Management, and Development

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/com.apple.dynamic_pager.plist

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/com.apple.dynamic_pager.plist

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 thoughts on “Macbook Air: how to disable the Swap file (run your laptop faster)”

Comments are closed.