Purge the OS X disk cache to analyze memory usage

The disk cache in OS X can sometimes use a fair amount of the system's RAM, and clearing it can help you figure out how much RAM your applications and system processes are using.

The memory in your system, commonly referred to as RAM, is the work space the system uses to store executable code and loaded data. If your system is slowing down then you may be low on RAM. Although you can install more or free some up by quitting some running programs, first it may help to analyze how much RAM your programs actually are using.

If you open up the Activity Monitor utility on your system and go to the Memory section, you can see a pie chart of the system's physical memory usage, where generally the larger the green and blue sections the better.

When most people think of memory, as it is displayed in the Activity Monitor pie chart, they assume it is the physical RAM in the system; however, in fact the memory that is available to programs and system services is called virtual memory, which is a logical combination of both the physical RAM and the available hard-drive storage. Allocating virtual memory to a program allows the system to swap RAM contents to the hard drive and otherwise manage memory in useful ways.

In OS X one aspect of the virtual memory system is the disk cache, which is used to overcome the relatively slow speed of the hard drive by mirroring its filesystem blocks in memory. When a file is first read, its contents are stored in virtual memory and its blocks are copied to the cache, so when the system writes to the file it has the option to do so in the cache rather than waiting for the slower hard drive. At a later point a separate process synchronizes the modified blocks in the cache with their respective ones on disk.

Memory usage in OS X
After running the 'purge' command the system's inactive memory usage level drops, though its active usage also drops a small amount. Screenshots by Topher Kessler

Some operating systems create the disk cache in a separate allocation of system memory, but in OS X the cache is merged with the virtual memory system and is called the Unified Buffer Cache. This merge results in the cache (and therefore the observed virtual memory use) growing as files are accessed, which at times can result in considerable RAM usage.

As with other aspects of virtual memory, the Unified Buffer Cache should be dynamically unloaded as the system's demand for memory increases (in other words, when you open more programs or files), and therefore not require any user input to manage. However, though clearing the disk cache won't improve performance, it will prevent it from being included in memory measurements and therefore help you find out how much RAM your programs and system processes are truly using.

To do this, open up the Terminal, type the command "purge" at the command line, and then press Enter. This command will take a few moments in which it will stress the virtual memory system to unload cached disk blocks and free them up, and in doing so will appear to reduce the overall memory usage of the system. When it's done, the resulting memory usage by the system should better indicate how much memory your applications and system services are actually using, similar to the levels they used when the system was first booted.

Do keep in mind that while this command will appear to free memory, it will not increase system speed or RAM capacity. Rather, as I've mentioned in the past about other memory cleaner utilities , once you clear the disk cache or otherwise stress the virtual memory system to free up physical RAM, it will slowly begin to equalize and will soon return to its prior RAM usage levels. Therefore, instead of using the purge command or other memory management tools to "free RAM," it may be best to reserve these for determining programs' memory footprints and thereby isolating problems such as memory leaks, or determining if programs themselves are to blame for high RAM usage.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    ARTICLE DISCUSSION

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    Hot on CNET

    CNET's giving away a 3D printer

    Enter for a chance to win* the Makerbot Replicator 3D Printer and all the supplies you need to get started.