A common phenomena in my day to day usage (and several other's according to various posts throughout the internet) of OS X, the system seems to become slow whenever there is no more "Free" memory available. Supposedly, this is due to swapping, since heavy disk activity is apparent and that vm_stat reports many pageouts. (Correct me from wrong)

However, the amount of "Inactive" ram is typically around 12.5%-25% of all available memory (^1.) when swapping starts/occurs/ends.

According to http://support.apple.com/kb/ht1342 :

Inactive memory

This information in memory is not actively being used, but was recently used.

For example, if you've been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. This 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 Mail from the slower hard disk.

And according to http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html :

The inactive list contains pages that are currently resident in physical memory but have not been accessed recently. These pages contain valid data but may be released from memory at any time.

So, basically: When a program has quit, it's memory becomes marked as Inactive and should be claimable at any time. Still, OS X will prefer to start swapping out memory to the Swap file instead of just claiming this memory, whenever the "Free" memory gets to low.

Why? What is the advantage of this behavior over, say, instantly releasing Inactive memory and not even touch the swap file? Some sources (^2.) indicate that OS X would page out the "Inactive" memory to swap before releasing it, but that doesn't make sense now does it if the memory may be released from memory at any time? Swapping is expensive, releasing is cheap, right?

Can this behavior be changed using some preference or known hack? (Preferably one that doesn't include disabling swap/dynamic_pager altogether and restarting...)

I do appreciate the purge command, as well as the concept of Repairing disk permissions to force some Free memory, but those are ways to painfully force more Free memory than to actually fixing the swap/release decision logic...

Btw a similar question was asked here: http://forums.macnn.com/90/mac-os-x/434650/why-does-os-x-swap-when/ and here: http://hintsforums.macworld.com/showthread.php?t=87688 but even though the OPs re-asked the core question, none of the replies addresses an answer to it...

^1. UPDATE 17-mar-2012 Since I first posted this question, I have gone from 4gb to 8gb of installed ram, and the problem remains. The amount of "Inactive" ram was 0.5gb-1.0gb before and is now typically around 1.0-2.0GB when swapping starts/occurs/ends, ie it seems that around 12.5%-25% of the ram is preserved as Inactive by osx kernel logic.

^2. For instance http://apple.stackexchange.com/questions/4288/what-does-it-mean-if-i-have-lots-of-inactive-memory-at-the-end-of-a-work-day :

Once all your memory is used (free memory is 0), the OS will write out inactive memory to the swapfile to make more room in active memory.

UPDATE 17-mar-2012

Here is a round-up of the methods that have been suggested to help so far:

The purge command

"Used to approximate initial boot conditions with a cold disk buffer cache for performance analysis. It does not affect anonymous memory that has been allocated through malloc, vm_allocate, etc".

This is useful to prevent osx to swap-out the disk cache (which is ridiculous that osx actually does so in the first place), but with the downside that the disk cache is released, meaning that if the disk cache was not about to be swapped out, one would simply end up with a cold disk buffer cache, probably affecting performance negatively.

The FreeMemory app and/or Repairing disk permissions to force some Free memory

Doesn't help releasing any memory, only moving some gigabytes of memory contents from ram to the hd. In the end, this causes lots of swap-ins when I attempt to use the applications that were open while freeing memory, as a lot of its vm is now on swap.

Speeding up swap-allocation using dynamicpagerwrapper

Seems a good thing to do in order to speed up swap-usage, but does not address the problem of osx swapping in the first place while there is still inactive memory.

Disabling swap by disabling dynamicpager and restarting

This will force osx not to use swap to the price of the system hanging when all memory is used. Not a viable alternative...

Disabling swap using a hacked dynamicpager

Similar to disabling dynamicpager above, some excerpts from the comments to the blog post indicate that this is not a viable solution: "The Inactive Memory is high as usual". "when your system is running out of memory, the whole os hangs...", "if you consume the whole amount of memory of the mac, the machine will likely hang"

To sum up, I am still unaware of a way of disabling Mac OS X from using swap when there still is "Inactive" memory. If it isn't possible, maybe at least there is an explanation somewhere of why osx prefers to swap out memory that may be released from memory at any time?

link|improve this question
I've got this problem, illustrated by these Activity Monitor numbers: bassistance.de/i/f2322d.png Barely any free memory, but plenty of inactive memory. Instead of reclaiming that, OSX prefers to start swapping heavily, as you can see by the 40 GB page outs. – Jörn Zaefferer Jan 23 at 14:47
feedback

4 Answers

There is currently no easy way of adjusting the swappiness (or so it is called) behavior of macos X. There are a few hacks available though (requires developer account & SDK):

http://cestdelamerde.com/archives/22-Killing-Mac-OS-X-Swapping-How-To-Disable-dynamic_pager.html

http://dropsafe.crypticide.com/article/3848

Good luck!

Postscript. I guess you may want to read this answer (also by me) to have a more general review of what active, inactive and other memories in MacOSX: Wired Memory vs. Active Memory in OS X

link|improve this answer
Thanks for your replies. Adjusting swappiness would definitely help - bummer that it aint easy! Regarding the first link, I'm really looking for a hack that doesn't include disabling swap/dynamic_pager altogether and restarting, since that requires too much effort in order to disable swap usage, as well as leaves the system unstable when approaching low amount of inactive and free memory. The 2nd link refers to the problem of ever growing swap files. While and important issue especially when one is going low on swap files, it is not really relevant to my question in this thread... – Motin Aug 1 '11 at 9:29
feedback

Not a permanent solution, but at least it can help reclaim some inactive memory, potentially avoiding the dreaded swapping: http://itunes.apple.com/nz/app/freememory/id460931672?mt=12

The tool is free and pretty easy to use. Once started, select its "Free memory" option in the system toolbar/menu.

Unlike the ActivityMonitor memory display, it only shows free memory, which seems to be a better indicator of swapping going on or not.

link|improve this answer
I have been evaluating this app since your suggestion about two months ago. It seems that this app indeed does increase the amount of "free" memory through various tricks, including aggressively claiming lots of memory for itself, then releasing it. However, this causes just the same amount of memory that it "freed" to be swapped out. This is to say it doesn't help releasing any memory, only moving memory contents from ram to the hd. In the end, this causes lots of swap-ins when I attempt to use the applications that were open while freeing memory, as a lot of its vm is now on swap. :( – Motin Mar 17 at 12:45
feedback

I bet there's no right answer to this. It's the same with the memory which remains active when you put your Mac to sleep, it just grows and grows with every sleep you make.

On the "fun" (a.k.a. expensive) side, you can upgrade your memory or replace the HDD with an SSD, so the swapping won't be that of a performance hit. I chose first option, as Corsair memory is now available at a reasonable price.

16GB RAM

link|improve this answer
feedback

Starting with OS X 10.5 there are evident memory management problems in MAC OS X. The web was already then cluttered with complaints about system slowing down dramatically after some time. Back then i had slower machine, Mac Mini with 1GB RAM, so i (wrongly) concluded that it was due to inferior hardware.

Now i have 2010 MBP, core i7, 8 GB RAM, dual GPU. Mac os X Snow Leopard was pain, but after migrating to OS X Lion, working some serious stuff on MAC started to be a nightmare.

I finally managed to reproduce the problematic scenario, so i run the test and recorded the screen, into video.

MAC OS X Lion performance problem - broken memory management

I run the tar+bzip command, which is basic unix stuff, on the large amount of picture files, in my Pictures/ folder. Just before start, i run the "purge" command, to delete inactive/cached program data.

You can see on the video that free memory starts to drop very fast, and inactive is constantly rising. If you take a look at "bsdtar" command, it takes only a fragment of RAM, so the problem is not in this process. You cannot say that it is a program memory leak, because then the problem would not be in inactive ram, rather in active/wired.

When the free memory dropped below 100mb, i started some apps, like Safari, iPhoto and MS Word, and you can see in the video, that it takes even minutes to start an app, when normally (when there is free RAM), it would take some 3-5 secs to load.

I run the same scenario and the same commands on my Linux Centos 6 box, no problem there ! Memory usage is some 10-20mb, no problems with cache/buffer.

The memory management must be very broken in Mac OS X !

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.