Mac Virtual Memory – What it is, the Swap Location, and How to Disable Swap in Mac OS X

Oct 8, 2010 - 36 Comments

mac virtual memory I was asked recently about the Mac OS X swapfile, specifically how to disable Mac OS X swapping entirely. I decided I will take this opportunity to talk a bit about Mac virtual memory (swap), it’s location in the Mac file system, and also to explain how to disable it.

Mac OS X Swap aka Virtual Memory

You may recall that in older versions of Mac OS (OS 8 and 9) you could manually disable swapping, then called Virtual Memory, by just adjusting a setting in the Control Panels. Mac OS X is a bit different because it’s built on top of a unix core which relies heavily on swap files and paging for general memory and cache management. Because of this, swap is actually more important now than it was in prior versions of Mac OS.

Basically when your Mac needs memory it will push something that isn’t currently being used into a swapfile for temporary storage. When it needs accessing again, it will read the data from the swap file and back into memory. In a sense this can create unlimited memory, but it is significantly slower since it is limited by the speed of your hard disk, versus the near immediacy of reading data from RAM.

If you’re curious, you can check Mac OS X’s virtual memory usage using the ‘vm_stat’ command, or by using the Activity Monitor (often erroneously called the Mac task manager by Windows converts).

Mac OS X Swap File Location

If you’re curious where the swap files are stored on your Mac, they’re located at:

/private/var/vm/

This directly also contains your sleepimage file, which is essentially what your Mac has been storing in memory prior to system sleep. This file is read again when you wake your Mac up to return to it’s previous state. Anyway, back to swap files in the same directory: they are named successively swapfile0, swapfile1, swapfile2, swapfile3, swapfile4, swapfile5. You can see them for yourself with the following command:

ls -lh /private/var/vm/swapfile*

The swapfiles are generally staggered in size, ranging from 64MB to 512MB.

Disable Mac OS X Paging / Swap

Caution: I would highly recommend against modifying how Mac OS X handles memory management and swap files. Unless you know exactly what you’re doing and why, this is not a recommended adjustment. Again, if you don’t know what you’re doing, do not mess around with Mac OS X’s swapfiles or paging ability!

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.

Your next step would be to remove the swapfiles that are currently stored, they are generally pretty large (it is your virtual memory after all) and take up a fair amount of disk space.

sudo rm /private/var/vm/swapfile*

That’s all there is to it.

.

Related articles:

Posted by: Manish Patel in Mac OS

36 Comments

» Comments RSS Feed

  1. thecrud says:

    I want to relocate /private/var/vm/ to a 16 gig optane
    Can I relocate to a swap drive/ partition?

  2. John R says:

    I’ve been getting “Mac OS X startup disk has no more space…” Messages for a week now with 100GB of space available. On restart the free space jumps back up to 160GB free but can drop to 100 in a matter of hours with almost nothing running. Using OmniDiskSweeper I found 47 swapfile(s), each over 1GB! As the swapfile(s) propagate, my Mac runs slower and slower, becoming almost useless until the “no more space…” messages forces a reboot. I’ve never had this problem before and have never seen the “no more space…” message before unless the free space dropped into the MB range, certainly not with 100GB free. Is there a fix for this?

    Thanks

    • ph says:

      What version of Mac OS X are you running? Is the Mac older or newer?

      New Macs with SSD volumes should not slowdown as much when swap is used.

      If you are running an older version of Mac OS X, and the Mac is new enough to support it, updating to a newer release may resolve the issue (Snow Leopard 10.6.8, Mavericks 10.9.5, El Capitan 10.11.3 are probably the best bets for Mac OS X overall)

  3. Vencil says:

    $ du -sh /private/var/vm
    6.1G /private/var/vm
    $ ls -alt /private/var/vm
    total 12713984
    -rw——- 1 root wheel 1073741824 Jun 13 08:40 swapfile1
    -rw——- 1 root wheel 1073741824 Jun 13 08:40 swapfile2
    -rw——- 1 root wheel 1073741824 Jun 13 08:40 swapfile3
    -rw——- 1 root wheel 1073741824 Jun 13 08:40 swapfile4
    -rw——- 1 root wheel 1073741824 Jun 13 08:07 swapfile5
    drwxr-xr-x 9 root wheel 306 Jun 13 08:07 .
    -rw——- 1 root wheel 67108864 May 30 10:07 swapfile0
    -rw——T 1 root wheel 1073741824 May 28 2014 sleepimage
    drwxr-xr-x 27 root wheel 918 May 28 2014 ..
    $ sudo dynamic_pager -L 1073741824
    Password:
    $ r ls
    ls -alt /private/var/vm
    total 2136216
    drwxr-xr-x 4 root wheel 136 Jun 13 08:41 .
    -rw——- 1 root wheel 20000000 Jun 13 08:41 swapfile0
    -rw——T 1 root wheel 1073741824 May 28 2014 sleepimage
    drwxr-xr-x 27 root wheel 918 May 28 2014 ..

    Certainly does wipe out the swapfiles! But how to make it a
    permanent configuration?

  4. dwasserman says:

    Can I change te location of swap file, for example creating a small partition? If yes, how?

  5. arlet bode says:

    Hey I wouldnt mess with the swap files because I wanted to save some space on my mac and it ended up freezing some days after. I had to restart it but it didn’t turn on. I had to take it to apple and had it reset. Maybe it was for something else, but just in case, dont delete them.

  6. bdens says:

    Thank you so much, this post gave me the info I needed to move my swap off the SSD and on to the secondary HD.
    —-
    Bdens

    Setup:
    Mac Mini 2011 2.5, 8g Ram, 2 HD mod, and Arctic silver 5 – thermal paste

    P.S. Apple, PLEASE use better thermal paste and Apply it correctly I lost 10c doing this!

  7. jtgans says:

    three_jeeps is absolutely right, guys — if you disable virtual memory, you’re going to have a bad time.

    Seriously, though, unless you’re doing arcane, deep black magic stuff with the kernel or OS research, DO NOT DO THIS! Very bad things can happen, from your system just being slow and unresponsive to DATA LOSS from the system memory management routines forcibly killing off processes when memory has run out for allocation requests.

  8. Disabling VM is a bad idea. A better way of doing things is to force the dynamic pager to do garbage collection with the command

    sudo dynamic_pager -L 1073741824

    Details at

  9. Hank Roberts says:

    Has anyone done this?

    Tell us how this has been working out — is the warning from ‘Three Jeeps’ above scaring people off?

    I’ve got 12GB of RAM on a current Mini, and am puzzling over whether this disabling VM is a good idea.

    Or, whether increasing the size of the swap file instead (how?)

    • Tesselator says:

      I’ve been using my MacPro (32GB RAM) like this for about a week now. I notice SIGNIFICANTLY less drive chatter and many many operations are noticeably faster. Especially OS operations like browsing around in folders with hundreds or thousands of images in each. Icons once displayed are actually instant… I don’t mean fast either. On my system they were fast before. This is instant. Poof! A maximized window populated with any sized icons of RAW (or any kind of) images just appears and scrolling to the bottom of multiple folders each containing 4,000+ images works the same way. Browsing images in LR was sped up by about 10 or 20% too. Good news for those who like LR. Bridge was already very fast (about 6 to 8 times faster than LR) for poking around in image folders but it too became slightly faster. It’s too fast in the first place to measure proper differences but doing the best I could with my stop-watch it’s about double the speed with 1st time page displays (from about 2s to about 1s) and instant every time after that – for hours and hours and hours… and hours…

      I don’t recommend doing this with only 8GB or less however. But if you have 24GB or more then I feel comfortable recommending running your machine full-time with the dynamic pager turned off. 16GB would probably be OK too. It works on my older 12GB mac pro system just fine. This is why “with lots of RAM” is in the title. Furthermore you should probably run some kind of memory monitor in the BG. I’ve been using MenuMeters since, like, forever… so I didn’t need to add anything to the installation.

      I guess this will help with all versions of OS X from about 10.5 on up – which is when the paging system in OS X became slightly ridiculous.

  10. Simon says:

    I have a swap file of 209 GB.
    How to set a maximum size for it?

  11. three_jeeps says:

    I just read this article and the majority of posts and all I can say that it is extremely bad advice to turn off your swap space. To be blunt, this is a very stupid idea. The entire operating system is built around the idea of a backing store for RAM – that is basic Operating System Design 101. Memory allocation and deallocation use some fairly complicated algorithms to provide low-latency response to having multiple applications loaded, run, and then deactivated. Having a very small amount of RAM (e.g. 1 Gig) causes the allocation/deallocation algorithms to be somewhat inefficient, causing bottlenecked or wedged applications. The more apps one runs, the worse the situation becomes. Your system will literally crawl or wedge. Buy as much RAM as you can afford, and increase your swap to something like 10GB or more. Disks are cheap and althought not as fast as RAM, it will not slow your system to a crawl.If you are worried about killing your SSD, you have two choices: max out the RAM and buy a mechanical HD, or, disable swap and watch your system crawl…..To be honest, the likely hood of destroying your SSD is very slim.

  12. Nick D says:

    DO NOT disable dynamic_pager!

    I had it disabled for a few weeks and noticed that my Mac’s wired memory usage would grow out of control. Turns out that dynamic_pager is involved in cleaning up wired memory…

    “Wired memory is not immediately released back to the free list when it becomes invalid. Instead it is “garbage collected” when the free-page count falls below the threshold that triggers page out events.”

    http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html#//apple_ref/doc/uid/20001880-99714-TPXREF106

  13. Ken says:

    How can we tell if this worked? I ran the terminal commands>Restarted>Opened Activity Monitor>Chose the System Memory Tab>VM Size: around 190.55GB… Shouldn’t this be 0?

  14. […] 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 […]

  15. […] apps open right now and I’m not even close to hitting virtual memory (you can read more about virtual memory in Mac OS X here). Anytime you can avoid using virtual memory your Mac will perform faster since it does not need to […]

  16. […] Virtual Memory (commonly called swap) […]

  17. […] Mac OS XはSnow Leopardになってからメモリマネージメントが大分賢くなったけれど、依然としてswapの発生頻度はかなり高い。場合によってはInactiveが数GBあるのにもかかわらずswapを使い始めたりするので、4GB以上搭載していることが当たり前の今は、思い切ってoffにしてしまうのが良い。 今まではswapを無効化するために専門的な知識が必要な部分が多かったのだけど、最近簡単な方法を見つけたので紹介する。下記コマンドをTerminalで実行し、再起動する。 […]

  18. Mac Buff says:

    How does OS X keep track of the location of information in the swapfiles? In some systems there is a page table (sometimes actually a tree), but I’ve not been able to locate that in OS X (version 6). Any help out there?

    • Mac N Cheese says:

      Mac OS X and virtual memory. While it is true that Mac OS X is built on Unix base, the management of virtual memory by OS X is not akin to raw Unix or Linux vm management since virtual memory allocations occur via the microkernel API (Mach).

      The Darwin and osfmk source tree does not directly tie to any PPC hardware or x86 hardware related to paging, so that if you’re expecting to find an operating system structure (exposed and accessible through _asm() or c/c++ lib), you will find none. In fact, if you attempt to access memory that is not “pre-allocated” by Mac OS X, it achieves a “bus error” and fails whereas some Unix OS would allocate another VM page and continue. In cases where the kernel can not create more VM for itself, it enters a hard stop and crashes (why you should leave VM “on”).

      The latter is also why porting “other Unix” code to Mac OS X is a chore if the c/c++ source is not well-behaved and cooperating with OS API to allocate VM. These “rules” prevent code allocating beyond (overrun) established code and data boundaries (especially the heap and stack).

      Also, to thwart “hacking,” the structures used to govern memory allocation are not exposed in c/c++ API libraries, not even for _setjmp() and _longjmp() functions.

      Another layer of protection is that virtual memory init begins during Open Firmware initialization and setup of context which is based as “values” for Mach-O to use.

  19. […] Virtual memory in any operating system isn’t something you should really mess with, as the operating system likes to do it’s own thing in terms of handling it’s own memory management. That being said, if you want to disable virtual memory altogether on your Mac because you’re foolhardy and/or have oodles of RAM, then by all means, go right ahead. […]

  20. Sithus says:

    Is there a way you can increase VRAM?

    • BjarneDM says:

      VRAM is used as needed and taken from your HardDisk. Thus there is no way to explicitly – and also no need to – increase the amount of VRAM.

      If you find you are in need of serious amounts of swap the best option for you will be to increase your amount of physical RAM. Or take a serious look at how many programs you have running.

      You can improve the performance in several ways:
      1) dedicate a partition on your HardDisk
      2) dedicate a whole HardDisk – the faster the better

      Dedicating a partition or HardDisk also has the advantage that if a process runs amok it’ll be restrained to there.

      • Mikee says:

        Hi,

        Very interesting post.
        My Mac is becoming sluggish, especially with “heavy” apps like Firefox 4 (which remains a memory eater…), or LibreOffice. As the harddrive gets full, the sluggishness becomes more and more noticeable.

        So, as you pointed out, I wonder why Apple didn’t take back Unix/Linux precepts like automatically create a swap partition during first install???

        Swap partitions are normally formatted “raw” (without enhanced filesystems) to assure io speed. If I shrink my actual system partition to add a swap partition, I will be force to use HFS on it? Or there is a way to format it for swap?

        Next step: how to tell the system that its swap is now longer located on /private/var/vm/ ? Will a symbolic or hard link suffice?

        More important: if I move/link the /private/var/vm/ folder, you said that it’s also the place for sleepimage, what should be the size of the partition? Size of physical memory in bytes plus swap files? But how to anticipate the system’s need with swapfiles?

        And also: why it seems not possible to adjust (like with mkswap, and swap on/off) the size of the swap? As computers are shipped with more and more memory, it could be useful (especially for SSD) to reduce (shrink) swap space/size/use.

        Regards.

  21. digital says:

    This article isn’t really complete without telling people how to re-enable it..

    • sault says:

      How to re-enable?

      • QQ says:

        change ‘unload’ to ‘load’ should work in the launchctl command

        you just need to re-enable the kernel loading the page daemon

    • Azziza says:

      Thanks for the article. Can anyone tell me if we can delete previous swapfiles? I’m not too clear on that.

      I’ve got Swapfile0 to Swapfile5 with 3 files being over 250mb. Would like to delete 0-4 but don’t want to get in over my head… Thoughts?

  22. EldRick says:

    I take it that this is a temporary adjustment, and will be negated on reboot?

  23. Nick says:

    This will increase the life of an SSD because you can reduce the read/writes to SSD when you disable the pagefile. If you don’t have at least 8GB RAM you’ll get strange errors.

    The big question about Mac+SSD is when TRIM is coming to Mac OS X, if I could get a TRIM supported MBP with 16GB RAM I could ditch my Mac Pro for a field demon.

    • Sam says:

      No, this isn’t true. It will barely make a difference on the life of your SSD, and Mac OS does much better when you allow it to use swap space as it wishes.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site