56

I came across a thread about installing an SSD drive in MacBook Pro.
Someone in the comments also recommended to disable the swap file but didn't say why:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
sudo rm /private/var/vm/swapfile*

I wonder why one would want to disable the swap file in Mac OS X.
Does it increase performance? Stability?
Any downsides?

Finally, does it make any sense to disable swap file when not using SSD?

6
  • 13
    It's often disabled by the less knowledgeable because they heard on some blog that performance shot up threw the roof. SSDs also provide a nice testbed because they are inherently fast. Don't do this. It will decrease stability, and over time lead to catastrophic effects. To this day, I have not see any real data (valid and reliable) showing a performance increase. And it is a critical component of an OS, whether you have 2 GB or RAM or 120.
    – user10355
    Commented Sep 8, 2011 at 16:53
  • 3
    What catastrophic effects? Just curious... it seems to be making my machine (4GB MacBook Pro) a ton faster. For example, when I open new terminal windows there is no longer any blocking... it's instant where it took several seconds before. And I was able to open twice as many apps as I would normally use and nothing bad happened.
    – orange80
    Commented Sep 16, 2011 at 19:37
  • 4
    orange80: The usual cause of slow terminal windows is the login process looking up the previous login, which can be fixed by hushlogin: apple.stackexchange.com/a/54167/22570 -- did you happen to reboot after changing swap settings, maybe? There's no reason that terminal window speed would be improved by removing swap.
    – Ken
    Commented Aug 2, 2012 at 20:42
  • 1
    In Super User, with an accepted answer: Disable Mac's dynamic_pager? (2012-04-27) Commented Apr 27, 2013 at 12:14
  • 1
    I want to disable the swap because I want to protect my ssd from being written too often.
    – Qian Chen
    Commented Jul 9, 2020 at 6:52

15 Answers 15

44

wonder why one would want to disable the swap file in Mac OS X. Does it increase performance? Stability? Any downsides?

I suppose people would do it for an SSD drive because those drives don't have the same lifetime as standard magnetic spinning-disk drives. There are a more limited number of write cycles, so presumably using them with a swap file would use up these cycles more quickly.

Finally, does it make any sense to disable swap file when not using SSD?

If you have a lot of RAM, this would possibly speed up performance, but it's not a good idea in general. If you do run up against the memory ceiling, things are going to get flaky.

Also, wired memory isn't released when it becomes invalid, it is only released when a page out event is triggered, which won't happen if Virtual Memory is disabled. So it won't take long for all your memory to be gobbled up even if it is no longer used.

6
  • 4
    The mac virtual memory system is very lazy in that it only creates the physical swap files when it is time to swap out actual memory. Whatever speed up you might see by not having to delay things to accommodate a write to swap would almost certainly be offset by the previous speed gains of having swap available in the first place. Having swap makes macs faster. The rest of the answer is spot on - nicely done!
    – bmike
    Commented Sep 8, 2011 at 16:49
  • 2
    @geotavros The virtual memory system in Mac OS X does not work the same as Windows. With 4GB of memory, trust me, your Mac will work faster with Virtual Memory enabled. Disabling VM will only cause you headaches and system instability.
    – ghoppe
    Commented Mar 20, 2012 at 15:06
  • 5
    FYI, a modern SSD with write-leveling and a decent capacity (128 GB), written to continuously, will have a write-cycle-limit measured in centuries. That was a reasonable fear in 2000 with small, primitive SSDs, but not any more.
    – Ken
    Commented Aug 2, 2012 at 20:29
  • 3
    For a MacBookPro5,2 with 8 GB memory the performance improvement is dramatic. For Snow Leopard or Lion in a VirtualBoxVM the improvement is astounding. In all three cases the OS appears to use memory in a way that minimises the risk of reaching the 'limit' (where all things effectively stop) but that risk remains, so it's sane to observe usage in Activity Monitor. Commented Aug 11, 2012 at 18:37
  • 2
    For information VM (or Virtual Memory) should not be confused with swap file. Windows, OS X, *BSD, Linux, many Unix are all using virtual memory. Usually the archiecture of this OSes gives a VM per process, the process allocated memory within this area and the OS takes care to put this allocated memory on physical RAM or swap as the OS deems so. Having or not swap can change in one way or the other performances for various criteria (speed, responsivity, etc.), this will depends of each one of us workload. As English people say: your milleage may vary! And greatly in this case.
    – Huygens
    Commented Feb 24, 2013 at 14:14
27

The reason they disabled the swap file is likely paranoia about SSDs having a maximum number of writes per block. Swap files can be written to frequently, possibly causing disk fragmentation (as well as file system fragmentation) and, eventually, possibly causing the disk to fail sooner.

That said, this is definitely a case of premature optimization. It is almost always a HORRIBLE, HORRIBLE idea to disable your swap file. The swap file is there for when your running programs consume more memory than you have physically installed. If you disable it, you may not be able to run as many programs at the same time (and the limit is far lower than you'd expect) or you may have issues (like freezing and having to hard-reboot the machine) when you run out of memory.

The latter I've experienced (with the swap file enabled) when running out of disk space. I had a 120 gig drive with ~3 gigs free. While playing Civilization IV (a large, full-screen game), the system ran out of hard drive space (~50 megs free) and presented the "kill apps now" window. This was hiding behind the full-screen game, so the system just locked up. If I hadn't been able to ssh in from another Mac to kill several apps remotely, I'd have had to hold down the power button.

9
  • 6
    I'd like to echo what a bad idea this is. Swap is used even if you have 16 GB of RAM. Not as much, but a tiny bit is already written into swap. Always. It's a safeguard. And while the OS may run without it, you cannot be sure how 3rd party apps behave. HD get written to. It's a fact if life. And no, there will be no "real world" performance increase. That's a placebo.
    – user10355
    Commented Sep 8, 2011 at 16:48
  • 4
    @cksum In some specific server scenarios, sysadmins will disable the swap file because it does make that specific configuration faster, but they don't do that without testing to make sure it both (a) won't run out of memory and (b) it actually is faster. But that's a server run by professionals: it's never necessary on a consumer machine.
    – Cajunluke
    Commented Sep 8, 2011 at 17:02
  • @cksum I could agree with your first sentence, you are correct with the second, third and fourth one, but there rest is rubbish. An OS does not swap for "safeguard", an OS is swapping to make more available memory to active process that would require it or to speed up I/O by buffering data, so long unactive memory can be freed in the RAM. When you develop an application, you request memory to the OS, this memory is part of your virtual memory usage, as a developer or application you do not know if the OS put that memory or part of it in the physical RAM or swap. (to be continued)
    – Huygens
    Commented Feb 24, 2013 at 14:20
  • So if a 3rd party app requests more memory than there is available, the OS will give it an error. This is independent of having swap or not. Having swap will put just the limit higher, that's it. As for performance, if you have 2 users logged in, if the active user use a video editor with huge files, the OS might start putting memory pages of the inactive user in the swap to speed up the tasks done by the active user. You get better performance in terms of speed in this case. However, switching to the other user will be slower, the OS has to read back the swap. This is not a placebo!
    – Huygens
    Commented Feb 24, 2013 at 14:25
  • 1
    I once (in linux) forgot to set up a swap partition when setting up. When I was getting hanging, I thought it was a kernel bug - turned out it was the lack of swap memory while playing mincecraft. use swap.
    – Wyatt Ward
    Commented Mar 16, 2015 at 4:13
11

You have two great answers that explain why this is a bad idea in almost all cases where the system can write to a storage device.

The reason you can disable swap is so the system can run on read only media such as a CD, DVD, locked network image using NetBoot or a read only USB fob like the new Lion installer or recovery fob that ships with some macs that lack optical drives.

Having virtual memory means that the system can run substantially faster and use less RAM when many programs use the same common library code. Each program thinks it has a private copy of all the code it needs, but when virtual memory exists, the system has a nice way to map the same physical RAM to different programs.

Disabling swap prevents the mac from using a tiny portion of your drive as a cache for some memory that has been calculated but the program wasn't smart enough to write those results itself to a proper cache.

If you have a backup and time to experiment, why not turn it off and see how your experience changes.

I would keep my swap on even if I knew for certain I had to pay for a new drive annually. The speed up that swap gives me is HUGE. This is not always so, and at times when the system is thrashing or paging - you can have the opposite. Many people choose to run without swap so that they get a memory allocation error rather than let their mac start paging since that can slow down a machine when things aren't running smoothly.

Your case with a SSD could be different, so do let us know if you try it.

3
  • 2
    I find the opposite, a huge speedup when dynamic_pager is disabled. The screenshot currently at apple.stackexchange.com/a/60661/8546 shows virtual memory size and page ins measured whilst that daemon is disabled (whilst swap files are not used). Commented Aug 14, 2012 at 15:41
  • What precisely is being measured to determine a speedup? (Or are you seeing the absence of lags or delays in the UI or something else that's not easily quantifiable or measurable?)
    – bmike
    Commented Aug 14, 2012 at 15:43
  • Without measuring: with Lion, most things seemed noticeably faster – enough for me to use the word huge. More recently: 10.8 in a VirtualBoxVM, for example, to log in and resume Activity Monitor takes sixteen seconds with dynamic_pager, twelve seconds without. For a broader and more measured comparison we might use Phoronix Test Suite or Geekbench. Commented Aug 14, 2012 at 16:23
9

I'll be the naysayer here: I disabled swap on my late 2008 unibody macbook pro and it improved performance noticably: Less waiting, faster switching between applications.

Mind you, I keep an eye on my free memory and I have 6GB installed. The few times that I hit the memory limit, the system crashed :-D

On my new MacBook Air I wouldn't dream of disabling swap, it's already fast with it enabled and it only has 4GB to begin with.

Another good reason people disable swap on an SSD is because they get tiny SSDs and need all the space they can get. In any case, don't do it.

2
  • Awesome to have real experience where this worked. What was the RAM commitment on the machine when swap was disabled? What happened when you opened more apps than would fit in RAM? How much RAM did you have and what apps would run in that space comfortably without errors?
    – bmike
    Commented Nov 26, 2011 at 15:33
  • 1
    @bmike, not sure, the system crashes :-D, 6GB and all the ones I use :). Aperture or XCode are real memory hogs and they could sometimes tip over the system but Skype, Mail, Adium, Safari, Chrome, iTunes and OmniFocus all fit nicely...
    – w00t
    Commented Nov 29, 2011 at 20:14
6

My experience is similar to w00t's. I have disabled the swap file on SL and Lion because the performance increase works for me. Wired usage does get out of control after a week or so but a simple reboot fixes that. I do not have a SSD but a slow 5400rpm disk. SSD lifespans aren't actually as bad as people try to make them out to be though. If you're doing it simply because you're afraid of wearing out your SSD please see: http://www.anandtech.com/show/4159/ocz-vertex-3-pro-preview-the-first-sf2500-ssd/2 and http://communities.intel.com/thread/21717?tstart=0 Intel rates their drives for over 20GB a day, every day, for up to 5 years on a 80gb drive. Bigger sized drives will last even longer. The average user doesn't even use 10GB/day.

I make sure to monitor my 4GB memory usage with iStat Menus and use an Automator script with the "purge" bash command (requires Xcode to be installed) to clear out inactive memory and open up more free memory frequently. The few times I have had apps with memory leaks use up all the RAM, I did have to do a hard reset. The rare occasions of that happening and the speed up in general makes it worthwhile to keep the swap file disabled for me. I realize that I can't have as many apps open at once because of this as stated by others but I'm able to work with that.

1
  • Memory Scope has an option to automatically purge. (if dynamic_pager is enabled, I disallow purges.) Commented Aug 11, 2012 at 18:57
5

Just throwing in my 2 cents for those who wish to disable swap BECAUSE of small SSDs: the sleepimage uses far more disk space.

And, for those concerned about the limited life of the SSD (particularly those with MBA's and hard-wired SSD chips that CANNOT be replaced), if your computer sleeps many times per day, the rewriting of the sleepimage may be a concern. My laptop sleeps a half dozen times/day easily. That writes 48GB of data to my SSD daily.

The solution in each case is to disable hibernation, and then delete the sleepimage file that is as large as your installed RAM (8GB in my case).

The downside risk: if your battery dies while your laptop is sleeping, your current state will be lost. This will be equivalent to a forced hard restart. No physical harm to the laptop, just a loss of productivity. If you disable hibernation, then run your battery low and can't charge, just have the code handy to enable hibernation!

To disable hibernation, in the Terminal, the command is as follows:

 sudo pmset -a hibernatemode 0

To enable hibernation, the command is as follows:

 sudo pmset -a hibernatemode 3

To delete the last sleepimage to free up SSD/HDD space (only makes sense if you turn off hibernation, else a new one will take up that space next time your computure sleeps):

 sudo rm -rf /var/vm/sleepimage

Source: man pmset

3

Disabling swap does increase performance. For example I regularly use Aperture and when swap grows performance worsens and when it goes beyond 1Gb I have to restart OS to clean it and restore performance. Note: machine has 6GB of RAM and NOT all of it occupied, about 3GB is free, but 1G is swap significantly degrades performance.

3

I've been disabling swap on machines for many years, both on OS X and other UNIX variants.

I use a tool to alert me when memory is running low, and never worry about my system crashing.

My iMac has 12GB, and with swap disabled I almost always have 4GB free, even with multiple development tools, apps, browsers etc. open

1
  • 5
    What memory tool are you using?
    – Dmitriy
    Commented Mar 29, 2012 at 15:05
2

The primary reason to disable the swapfile isn't speed - it's disk space.

If one is a user who routinely runs within 5 GB of a full drive, disabling swap will stop the constant nagging of running out of space; I routinely wind up with a 4+ GB swapfile from routine browsing.

The speed improvement is astounding on older machines; it's not that impressive on SSD equipped modern machines. (Under OSX 10.0 on an iMac, using virtual memory halved the speed of many operations, including copying files between drives).

3
  • 1
    That sounds more like getting the horn fixed because the brakes don't work. If the drive is too full, get some of the data off it.
    – Tetsujin
    Commented Mar 2, 2015 at 20:32
  • 2
    @Tetsujin The largest single file on that drive WAS the swapfile.
    – aramis
    Commented Mar 2, 2016 at 10:03
  • 1
    my point still stands. Less data, bigger drive, or more RAM & you wouldn't have to.
    – Tetsujin
    Commented Mar 2, 2016 at 10:08
1

The repeating pattern here is this:

Those who HAVE disabled swap file experienced improved performance (with sufficient physical RAM)

Those who have NOT disabled swap space are certain that it will not work, and something terrible will happen, and are ardently advising that you not do it.

This is due simply to two things:

  1. Outdated ideas about the scarcity and high cost of physical RAM which are no longer valid
  2. A really poor swapping mechanism in MacOS causing use of swap files when there is sufficient physical RAM.
3
  • 1
    If swap space was used ONLY when needed, then it would be great. However, that's not the case. I run 2gb and 4gb virtual machines on my MacOS. If I have 1.6gb of free RAM, I should be able to run a 1GB virtual machine (or other program), but I can't because the OS starts paging it out. As a result, I can only use about 85-90% of my RAM. If I want to get to ALL of my RAM, I need to disable paging. I do this on all my Windows machines, and would like to do the same on my MacOS. If a program runs out of RAM, then it will fail. That's what I want to happen. I don't want other, running applications
    – user26622
    Commented Aug 2, 2012 at 19:03
  • 3
    YMMV. I don't disable swap anymore as I've not found it to help performance any on modern, RAM-loaded machines. I used to in the past as it did help back in the bad old days. Nowadays not only do I NOT see any performance change by disabling swap, I DO see greatly increased application instability. Commented Aug 10, 2012 at 18:24
  • 1
    The undesirable thing that will happen is that a human will forget that they can't launch another program and in time, the machine will crash due to a low memory error. Whether this is terrible, humorous, educational depends on the timing, your backup frequency and tolerance for delays.
    – bmike
    Commented Sep 7, 2012 at 12:29
1

Just a personal experience.

Memory Amount    |
 4 GB            | Don't disable it, Mountain Lion itself boots with about 1 GB.
 8 GB            | Do if you don't run apps that requires more than 1 GB of memory, in short, virtualization software, Photoshop, and stuff.
16 GB or more    | Do it! Even Xcode or Final Cut won't use more than 4 GB of memory, at least as I've seen. Just don't run too many virtual machines though.

A quick note: If your Mac uses integrated graphics, its memory is shared with your main memory with kernel_task. Thus, you will have to subtract that amount from your total usable.

SSD users, it's satisfying to have more space, but it's important to know that virtual memory thrives on SSDs, you can get whole lot of performance gain from it. Does it hurt the SSD? Yes, but you will long replace your Mac before you notice.

1
  • 1
    I've personally disabled virtual memory on my Retina MacBook Pro with just 8 GB memory. Here's the math, the system uses about 2 GB memory counting integrated graphics. So that's 6 GB for me. I do Xcode for work, at that consumes no more than 2 GB memory. So I have plenty to spare.
    – Shane Hsu
    Commented Jan 30, 2013 at 1:55
1

Executive summary

A swap area (called swap partition, swap file or page file) is just a mechanism to augment the physical memory addressable by the OS.

If you are low in available physical RAM, and on today's machine, I would say if you have 4 GB or less RAM. Then you should probably have a swap. But this depends on your workload and how you use your machine.

If you have more than 4GB of RAM, there is a good chance that your system might never use the swap. So if the swap is enable or not, this won't change much.
Although this depends on other factors, the longer your machine will be running the more likely the OS will swap small unused portion of the RAM to the swap eventhough there is plenty of place left apparently. However, often the system use the "free" RAM for file buffers and other stuff, and some workload might make your file buffer use all available RAM.

Background information

Any way what would happen if you disable the swap and your memory is completely used? Well, the next time an application request more memory (like with a malloc or realloc), it will receive an error! If it is properly coded, the application will detect it, but handling such an error and terminating the program gracefully could be a challenge still! However, many developer don't test for successful memory allocation and often the application crashes as soon as this non allocated memory is going to be used. So there is a strong chance that the application will crash, and actually that many application will crash because you have more than one application usually running at the same time. Hopefully, the kernel itself should be immune, but in my experience it was not always the case.

If you have swap, you will have the same behaviour, but you have a higher limit so chances are that you won't reach that limit.

I have personnaly 2 machines with 8 and 16 GB of RAM. My personal usage of these machine should never bring me close to use all of this RAM (today! perhaps in a few years that could be different). The only reason I would hit the physical RAM limits is that a process got rogue and is consuming all the RAM. Having no swap is a relief, the kernel will promptly kill the rogue process as soon as it hit the RAM limit. If you have swap, you will get a painful experience (maybe not on SSD...) as your system will swap and swap and swap and become sometimes totally unresponsive until hopefully your kernel kills the culprit!

Advises

You should not be scared by people telling you something horribly wrong might happen if you remove swap. If you do so, your physical address space will be reduced, much like if you would go from 4 GB of RAM to 2 GB. Would you do such a thing? Then you should not remove the swap. You can however decide to limit it, and that would be wise.

If one thinks his workload might bring him to use close or more than the available physical RAM, or if one needs a lot of I/O (read/write to disks), then swap should be kept.

When swap should be kept, a user should try to set limits to the amount of memory a process can use. On a Linux system, one could use cgroups memory limits to avoid that some process gets out of control, I don't know the equivalent on OS X but that should be the way.

However, if one has more than enough available memory for his workload, then he could safely disable swap or he could keep it. It won't change much even in terms of performance because if the system does not use the swap, then you don't get hit by a performance drop (especially visible on an old rotational hard disk, less so on SSD).

Using the same trick as above to try to set memory limits per process or group of processes is also beneficiary in the case of more than enough available RAM.

My own example

I have an old PC with 1 GB RAM, there I do have a big swap: 4 GB so I can run Firefox and other application on it. I tend to use as little application (or tabs in Firefox) as possible to have a correct user experience though.

I have a small server with 8 GB RAM. There I know exactly my work load and I do not want my server to slow down because a process got rogue and is consuming all memory. There I have no swap and cgroup memory limit enforcing barrier, so applications or services can crash but not the kernel. Btw, this is a Linux machine.

Then, I have my Mac with 16 GB RAM. I am not the sole user of it, and we would not mind waiting when switching user. There we do have swap, it is difficult for me to plan the work load of other users of this computer. Even if I would be the only one I would use swap. I often run several VMs and use memory consuming applications, but not all at he same time, so I would not mind the OS swaping in an unused app so I can get faster disk I/O or launch yet another VM. Althoug up to now, I haven't seen this machine swapping!

0

I just disabled the swap file on my 2011 Macbook air (4GB RAM, 128GB SSD) because I was sick of the swap file slowly eating up all my remaining HD space and then having to restart. This way, the idea is that I will just run less programs.

I'll see if that really works, or if I will have to restart anyway to clear the "Wired" memory, but for now it appears to be working well. The one time the memory got really full the computer slowed down just as much it does when my SSD runs out of memory - but it recovered a lot faster as soon as I quit applications. (And I was actually able to quit applications, which I usually am not when the SSD fills up).

I'll see how it goes stability wise, but so far I'm a happy camper. It also forces me to run fewer applications (and Chrome tabs) at a time, which is good for task focus.

0

Some say: disabling swap improves performance.

This is true. Any use of virtual memory is a big slowdown – even a fast SSD is a thousand times slower than DRAM; a disk more like a million times slower. You only need a small fraction of memory accesses to go to swap to make a big difference in run time. If e.g. 0.1% of accesses go to your fast SSD, that theoretically halves your speed. However, multitasking hides this – while waiting for your swap device, the CPU can be kept busy doing something else. So as long as you have a reasonable level of multitasking so something is ready to run when you have a page fault, the performance loss is not that extreme. Too much stuff with high memory demands running at once leads to thrashing: swap activity dominates performance.

But: without swap, if you run out of physical memory, that is like running into a wall. Possibly you can sprint faster if you reduce your weight a bit by not wearing your glasses; the logic of this doesn’t get better if you have really heavy glasses…

0

Why to disable the swap file in Mac OS X ?

Does it increase performance?

YES - because when #OS start use swap it decreasing performance ⬇️ -- BUT under 2GB it is OK ✅

many times I have 15GB file and macos don't know what have to do first , high disk IO , and after reboot windows are in Desktop:1 ❌

Stability? Any downsides?

YES - time to time #macos is freezed with black screen ❌ - BUT restart is stable and fast with full window position restore inside proper Desktops ✅

You must log in to answer this question.

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