Inactive memory and paging woes

Ripcord

Senior Lurker
So I've been concerned about Mac memory management for a while.

I've had 4GB of RAM on a number of different systems with 10.5-10.7. I have 8GB at work.

I'm constantly running out of memory and paging, though. It seems like Mac OS X doesn't release memory once used. For example, if I:

1) Start with 3GB of RAM free
2) Start Chrome and a bunch of tabs
3) Start iPhoto (with my massive massive library)
4) Start VMware and start a virtual machine

I can pretty quickly run the amount of "Free" memory reported by Activity Monitor down to zero.

Now, I close iPhoto.
I close VMWare.

This marks about 1GB as free. Only 1GB. I end up with 1.82GB of "wired" memory. Some of it is marked as Inactive Memory - 700MB in the test I just did.


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

"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."

Based on this, I assume applications should be able to use this Inactive memory even if they previously didn't own it.

Now I start up some other things, like iTunes and Firefox. The free memory goes away. Once it does, I start swapping to disk - Page Outs start rising and "Swap used" increases depending on what all I load. *Inactive memory never drops*.


My concern here is that after the system boots, I can generally keep about 3GB of Active Memory. That's 3GB for my apps to play with. After using the system for a while, the amount of Active+Free memory is rarely over 1.5GB, no matter how many apps I quit, and I seem to swap a whole lot more. It seems directly related to having opened and quit other apps in the meantime

Or to put it another way, here's a test:

1) Open Chrome and do things in a bunch of tabs. No Swap used, speed is nice and fast.
2) Close Chrome
3) Do a bunch of other things with the system. Open a bunch of apps and close them.
4) Quit all apps
5) Open iPhoto, which uses 1GB of real memory. Lots of swap used, speed is slow because I'm paging out all over the place.

I have 4GB of RAM, so even if some memory is permanently reserved in step #3 for some reason, I should have plenty of memory with no apps open to use an app that reserves 1GB of Real Memory, I'd think???

I have this general problem on all my systems. Same with my system with 8GB - after using the system for a while I start swapping out all over the place even with a few apps open that use only 8GB of Real Memory.

Plus, why am I swapping out when I have 730MB of inactive memory??

Anyone looked close into this kind of thing?
 
This isn't the only thread to talk about this stuff; there's been jillions, though none I've found that talks about exactly what I'm discussing, mostly just people talking about what the different "forms" of memory are.

I notice that this thread (http://macosx.com/forums/mac-os-x-system-mac-software/47981-inactive-memory.html) from 2004 correlates with the Apple description

Inactive RAM may be thought of as a first stage swapfile. It contains instructions and data that are not currently in use within the OS or an application, but is left in RAM in the event it is needed again. If more Active RAM is needed, the Inactive RAM will be reassigned and if it contains data for a currently open application, that data will be rolled out to a swapfile. Otherwise the memory will simply be overwritten.

So I'm wondering why, among other things, my Inactive RAM isn't being converted into Active RAM? Instead the system pages out and uses swap space?
 
Are you sure you are swapping? I thought Unix systems map the program file to memory, so in a way running programs seems to be swapping (file is used instead of RAM).
 
This is semi-true: "swapping" is not necessarily a bad thing, but excessive swapping in certain cases can be.

There are two types of "swap" going on: "pageins" and "pageouts." "Pageins" are when data is read back into memory from disk (or other parts of RAM), and can happen even when you haven't run out of free/inactive memory.

"Pageouts," on the other hand, typically indicate swaps to disk (though not 100% of the time). This happens typically when you run out of free/inactive memory, but can happen sometimes even when you have tons of free memory (for example, if an application has a hard RAM usage limit: if an application can only use 4GB of RAM maximum, but you have 16GB of RAM in the machine, you may see some disk swaps because the application may have exhausted all usable memory).

Are you seeing "pageins" or "pageouts"? Open the terminal and run "top" and check the status of the pageins and pageouts and note which one seems to have a high number...
 
Here's a test I did to illustrate the problem. I collected memory usage stats at different points after reboot and starting/closing apps. This seems to show:

1) After starting and quitting a bunch of apps, OS X converts a lot of memory to the Inactive Type
2) OS X doesn't seem to reclaim inactive memory in favor of swapping to disk, or for use by any application - leaving the memory useless.



1) After bootup:
Free: 2.34GB
Wired: 720.5MB
Active: 693.5MB
Inactive: 258.0MB
Page outs: 0
Page ins: 0
Swap used: 0

2) After starting iPhoto, iTunes and Firefox:
Free: 592.9MB
Wired: 834.4MB
Active: 1.96GB
Inactive: 649.2MB
Page ins: 0
Page outs: 0
Swap used: 0

3) After quitting iPhoto (so only Firefox and iTunes open):
Free: 1.22GB
Wired: 832.6MB
Active: 1.33GB
Inactive: 650.5MB
Page ins: 0
Page outs: 0
Swap used: 0 bytes


4) Launched a series of applications. Juuuust enough to get to the point where Free memory showed near-zero, and I started seeing Pageouts and swap space used.
Free: 27.9MB
Wired: 728.7MB
Active: 2.19GB
Inactive: 1.08GB
Pageins: 2.1MB
Pageouts: 4MB
Swap used: 54.1MB

5) Close all applications (except Firefox and iTunes):
Free: 876MB
Wired: 720.7MB
Active: 1.30GB
Inactive: 1.20GB
Page ins: 8MB
Page outs: 4MB
Swap used: 27.7MB

6) Restart iPhoto:
Free: 24.2MB
Wired: 837.3MB
Active: 1.98GB
Inactive: 1.22GB
Page ins: 8MB
Page outs: 227MB <--- iPhoto took longer to start and is much less responsive, presumably because of the Pageouts.
Swap used: 480MB

This is the point where I think it falls apart. I've returned to the same state as step #3. The applications themselves aren't taking up significantly more memory, but "Inactive" memory is much higher. Instead now we have no Free space and I'm paging out to disk significantly.

Why would I page to disk at this point? I should be roughly the same as step #3??

Why isn't it reusing Inactive memory? It seems like this is reserved and never released for other apps to use.


7) Keep those started, and start up a buttload of applications:
Free: 30.5MB
Wired: 755.0MB
Active: 1.97GB
Inactive: 1.28GB <---
Page ins: 214.5MB
Page outs: 491.6MB
Swap used: 918.6MB
 
I think this is showing that OS X has some serious problems with memory management? And this has a significant impact on system performance. iPhoto was way slower to start and way less responsive the second time I launched it, even though the system state was effectively the same as the first time I launched it.
 
Back
Top