Stop reading if your computer works fine
You are most likely not suffering from this problem because:
- You use your Mac like most normal people, or
- You’re running on an SSD (lucky you)
This symptom only shows up during very specific use cases; we observe it consistently when we have to start/stop/reboot our virtual machines many times during development and testing.
If you’re not suffering from this problem, please stop here. This technique is a pretty low-level tweak so you should be willing to understand the trade-offs1 before implementing it.
To understand the issue, first familiarize yourself with how OS X manages memory. Basically, once physical memory is exhausted, the OS starts paging to disk (yuck!), which is indicated by the number of page outs. To see if your computer is paging excessively, calculate the paging ratio: page outs ÷ page ins. If the ratio is less than 10% then you’re in pretty good shape.
Inactive vs. free
When an application releases memory back to the OS, instead of being freed directly, that memory is put into the inactive holding area. If the same information is requested again, instead of fetching from disk, the OS can just mark the requested inactive memory as active. This optimization works well most of the time since we tend to open and close the same sets of apps and documents in a given session. Over time, you’ll see inactive memory creep up and free memory creep down.
When you launch a new app that requires more memory than is available from the free memory pool, Lion is supposed to return some memory from the inactive pool back to the free pool so that no paging has to take place. In practice, however, Lion doesn’t always do this gracefully when large chunks of memory are released and acquired in relatively quick succession.
As we have come to know, disk access is the enemy of Lion.
Arriving at an acceptable compromise
1. Add more memory
For me the obvious thing to do was to bump my MacBook Pro’s memory up from 4 to 8 GB. This had the effect of delaying the symptoms, but the computer still came to a screeching halt when it started paging out gigabytes of memory.
purge to the rescue, sort of
Lion ships with the
purge command line utility that, among other things, flushes inactive memory. Using
purge, though, is reactive rather than preemptive. How it typically goes:
- Notice the computer is unresponsive
- Launch Activity Monitor to see that the computer is paging out like mad
- Launch Terminal
- Wait for 30 seconds to one minute, during which time you can’t use your machine at all
Far too disruptive!
3. Thank goodness for scripting and
To address the shortcomings above, we need to make these improvements:
- Instead of waiting for the system to become unresponsive, run
purgebefore free memory runs out.
- Make sure running
purgehas minimal impact on overall system performance.
We use a Python script to do the heavy lifting, invoking
- Free memory falls below a specified threshold (500 MB in this case) and
- Inactive memory is above a specified threshold (1 GB in this case)
When the conditions are met, you’ll see log entries in Console:
4/30/12 2:48:34.059 PM net.damacy.releasemem: Free: 298mb < 512mb 4/30/12 2:48:34.059 PM net.damacy.releasemem: Inactive: 3785mb > 1024mb 4/30/12 2:48:34.059 PM net.damacy.releasemem: Purging...
Be sure to check out the Gist comment for detailed instructions.
I’ve been using this script since early February, and have observed no problems. My computer’s paging ratio has stayed healthy and
purge seems to obey its
nice directive properly. According to my Console log,
purge was run five times in the past week.
Final verdict: happy Mac, happy user.
Feedback? Leave your comments on the Gist for this post.
Do a search for “os x purge” and you’ll find alternative implementations, such as:
- A hack for dealing with the fan/temperature issues in Lion
- Automatically reclaim memory from leaky programs
- How to recover memory on Mac OS X Lion for free