7

Background:

OSX keeps some information in RAM that is no longer used, but was recently used. This is called inactive memory, and is intended to improve user experience by speeding up some actions:

For example, if you've been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. Inactive memory is available for use by another application, just like Free memory. However, if you open Mail before its Inactive memory is used by a different application, Mail will open quicker because its Inactive memory is converted to Active memory, instead of loading it from the slower drive.

Sounds questionable (just how much can you possibly speed up Mail on a quad-core machine?), but at least that couldn't possibly hurt since such memory can be treated like free memory! Actually, it can:

In OS X, when the number of pages in the free list dips below a computed threshold, the kernel reclaims physical pages for the free list by swapping inactive pages out of memory.

This makes me question whether OSX' memory handling is in fact superior; after all the same page mentions that

Paging of any kind... affects performance negatively because it forces the system to spend a lot of time reading and writing to disk. Reading a page in from the backing store takes a significant amount of time and is much slower than reading directly from RAM. If the system has to write a page to disk before it can read another page from disk, the performance impact is even worse.

Issue:

This (paging out inactive memory and the subsequent severe performance degradation) is likely to occur even during trivial tasks such as multi-tab browsing, even if the number of simultaneously open tabs is kept within reasonable (both Safari and Chrome appear to be guilty of adding to inactive memory whenever a tab is closed).

The machine becomes hardly usable when this happens - even opening up Terminal and purging memory can take a few minutes.

Question:

Can swapping inactive memory be turned off? Or, with inactive memory not being essential, can it be disabled completely?

Alternatively, is there a daemon or a preferably Apple app available that monitors inactive memory usage (an OSX oxymoron) and when it goes over an arbitrary amount (half a gig?) a purge is performed?

5
  • 1
    seeing the tags I assume they still haven't fixed the paging of inactive memory in Mountain Lion?
    – Pieter
    Aug 20, 2012 at 12:53
  • @Pieter: apparently there's nothing to fix because current implementation is just as Apple intended :)
    – Oleg
    Aug 20, 2012 at 21:56
  • they confirmed this as 'working as intended'? I don't get what the benefits are of swapping inactive memory to disk.
    – Pieter
    Aug 21, 2012 at 12:36
  • "… subsequent severe performance degradation …" – despite what's in the Mac Developer Library, I'm not convinced that all degradations are severely negative. Suspect that in some cases there's a more negative effect from the OS attempting to restrain its approach to external swap files: Mac virtual memory: how to make OS X create swap (page) files as freely with a normal boot as it does with a safe boot (swapfile1 etc) Apr 27, 2013 at 9:37
  • 1
    @Graham Perrin: when I say severe, I talk from experience (albeit, not necessarily a shared one) - if you work with massive amounts of data and perpetually run out of HDD space before having it archived, the swap file gets more and more fragmented, increasing access time etc. (wouldn't be a performance issue with an SSD I imagine, but sounds like something that would shorten its lifespan). I'm not advocating this usage pattern but this is just something I keep running into.
    – Oleg
    Apr 28, 2013 at 0:00

3 Answers 3

7

I've been researching this very question for some time, and I've come to the conclusion that while there are several so-called "solutions" offered in various places, none of them really fixes the problem or makes the symptoms go away. The best I've found is techniques that change when inactive memory is swapped, such as the use of the purge command you referred to. I believe that MacLemon is correct that swapping can be disabled only in entirety and not selectively, and so disabling the swapping of inactive memory would in practice be equivalent to disabling virtual memory altogether—which could be a workable solution on a machine with really large amounts of RAM, but is impractical on machines with a low maximum RAM capacity, such as MacBooks or Minis.

The reason that none of the ad-hoc purging solutions make the situation significantly better is that there are really only two ways to force OS X to clear inactive memory: the purge command, or forcing allocation of all free memory (and since I'm not certain what method purge uses, these two may actually be more or less the same). purge, as you mentioned, takes a non-trivial amount of time to complete. Allocating all free memory simply accelerates the process by which the contents of inactive memory would be swapped out naturally, and so still takes up the same large amount of system resources. The only advantage of either of these solutions is that they allow you to choose when the swapping occurs, so it can be done before you would be normally trying to allocate that memory to something else. Whether that is actually useful to you depends on several factors, so the simplest way to find out is to just try it.

I've been testing a few of the memory-clearing utilities out there, and I've found that for me, manually forcing inactive swap requires more active monitoring of memory levels than is practical while I'm actually working, and using a utility that automatically forces the swap when free memory drops below a certain threshold is no better than letting the OS do it on its own, as I still have no control over when the swapping will occur and my apps will SPOD. So while there is an app that will do exactly what your alternative question asks for, that doesn't actually make the situation any less painful.

Until Apple's OS development team decides that the memory management system isn't working the way it's supposed to, and they figure out a way to make it work properly, the only real solution is to identify which apps are generating the most inactive memory and stop using them. In my situation, this has meant changing web browsers. I've been testing a variety of them, and Chrome is so far the one that seems to generate the least inactive memory, probably in part because every tab and every extension runs as a separate process, allowing the OS's native memory management to treat each one separately in terms of prioritizing swapping. Safari is the worst I've tried; starting with Safari 5.1, I could open a few tabs, do absolutely nothing, and watch the inactive memory rise rapidly in Activity Monitor—it would easily go from <1gb inactive to >3gb inactive within about five minutes, run the swap cycle, and then do the exact same thing. (Granted, memory that is doing nothing is exactly what makes it inactive in the first place, but it shouldn't create more inactive memory than was allocated active in the first place.) Safari 6 under Mountain Lion is a bit better, but not better enough to be worth switching back. Firefox, not being based on WebKit, ought to be better about this than either Safari or Chrome, but it has its own legacy of memory management problems, including a history of memory leaks, that make it no better in practice.

What would really fix the issue is if there was an option, likely a hidden option in the OS, telling the OS to simply dump the contents of inactive memory when needed instead of swapping its contents to disk. But I don't expect Apple to ever make such an option available.

1
  • "What would really fix the issue is if (the OS would) simply dump the contents of inactive memory when needed instead of swapping its contents to disk." As explained in my answer, the OS does exactly that. You just missed that part of the documentation.
    – Old Pro
    Aug 1, 2020 at 21:20
6

How much RAM does your Mac have? Also, browsers (especially Webkit ones) are known to use huge amounts of memory these days. Inactive memory is put to good use opposed to free RAM which just sits there consuming power. In the case of a browser this enables you to undo closing a tab and still have it's history preserved, etc.

You can completely disable memory swapping by running sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist and a restart. You cannot selectively disable swapping.

You can undo this by running sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist and restarting again.

5
  • "… Inactive memory is put to good use …" – +1. Apr 27, 2013 at 9:24
  • @MacLemon with 4GB of RAM and maverick os, I feel this mac is really slow. I have about 1.5GB inactive RAM.. Just trying your suggestion, but it seems not working well. any other suggestions? Sep 11, 2014 at 3:47
  • I never said it would work well and the first question was about the amount of RAM in the Mac. It answers your question. The proper Solution to your question is to get more RAM.
    – MacLemon
    Sep 11, 2014 at 6:38
  • I've 16GB (and SSD) and latest OSX is bloody slow when doing swapping. It terrible how it manage its memory.
    – kenorb
    Jul 21, 2016 at 16:22
  • Note: Disabling dynamic pager won't work on system with SIP engaged (System Integrity Protection).
    – kenorb
    Jul 21, 2016 at 16:25
3

Update 2020

This question was asked in 2012 and this answer was written in 2020, and some things have changed in the interim, but a lot has remained the same.

I believe most of this question, the answer from Quantumpanda, and the description from Apple Support article suffer from a misunderstanding of the nature of "inactive memory". The OP points to Apple Documentation that says

In OS X, when the number of pages in the free list dips below a computed threshold, the kernel reclaims physical pages for the free list by swapping inactive pages out of memory.

The Apple documentation said that in 2012 and still says that in 2020. However, that quote comes from the "Paging Out Process" section of the documentation; the OP missed the discussion of "Page Lists in the Kernel". The key sentence (for our current purposes) in that section is

If an inactive page has not been modified and is not permanently resident (wired), it is stolen (any current virtual mappings to it are destroyed) and added to the free list.

What the Support Article was talking about

For example, if you've been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. Inactive memory is available for use by another application, just like Free memory. However, if you open Mail before its Inactive memory is used by a different application, Mail will open quicker because its Inactive memory is converted to Active memory, instead of loading it from the slower drive.

In order to run a program, Mail in this example, the code for that program is read from disk and written to RAM, because the CPU can only execute code from RAM. The OS keeps track of the fact that the RAM is a copy of a file on disk, and it keeps track of whether or not that data in RAM has been modified. When the Mail program exits, the RAM copy of the file is no longer needed and ends up on the "inactive" list. However, if there is plenty of RAM to go around, there is no need to destroy the data in the RAM, so it is just left as-is.

Now if you run the program again right away, the OS does not have to copy the code from disk to RAM because it can see, by looking through the inactive list, that it already has a copy of the file in RAM, so it just moves that RAM to the active list and assigns it to the new running Mail program. This is much faster than re-reading the file from disk.

Mail is a bad example, though, because you are not often stopping and starting the mail program. However, there are dozens of small programs the OS runs in the background, such as to keep Spotlight indexes updated or keep your local data in sync with iCloud, that are frequently run and then quit when done. For these programs the time savings is much more significant.

The critical point: this kind of inactive memory is never swapped

This kind of memory, which is backed by a file on disk and not modified, is never written to swap, because there is already a copy of it on disk elsewhere. This is the kind of memory called out in the added quote above: "has not been modified and is not permanently resident". When there is not enough memory, this kind of inactive memory is not swapped, it is stolen and placed directly on the free list.

Inactive memory that is swapped is memory a running application still needs

The other way memory become inactive is if an application has not touched (read from or written to) it in a while. For example, if you are working in a word processor and have several open files, this might be information about one of the files you have open but are not working on. When memory runs low, this kind of memory is swapped out to disk to make room for whatever is requesting memory it needs right now that is causing memory to run low.

This kind of inactive memory must be swapped out because it contains data that is not saved anywhere else. The only way to prevent this kind of memory from being swapped out is to disable swap entirely, and that is almost always a poor choice. When you disable swap entirely, you reduce the capacity of the system significantly. If you limited the system to only running what it could run with swap disabled, it would perform just as well with swap enabled.

purge is a red herring

The purge program is a counter-productive red herring. It will force the inactive memory that would be stolen rather than swapped to be stolen right away. That may look good for the numbers, but it actually hurts performance in 2 ways:

  1. It takes time to run the purge program
  2. It removes the performance that is gained by keeping track of what the inactive memory contains and allowing it to be reused.

Bottom line: do not try to disable inactive memory swapping

You must log in to answer this question.

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