NOTE: This post is a work in progress

Recently there has been much discussion (and some controversy) on how memory is used in Windows 7, and more specifically, what exactly it is used for.  The issue at hand is the use of “spare” memory for caching disk files.  Does Mac OS X exhibit the same behavior?  Indeed it does, as modern operating systems utilize “spare” memory for the page cache.  The page cache is effectively a RAM based cache of files from the disk, also commonly called disk cache or disk buffer cache.  This memory in use (yet available quickly) can be mis-interpreted as unavailable if you aren’t aware of how the OS uses available RAM.

There are a variety of ways to determine how much memory is being used for this purpose

  • Activity Monitor
  • vm_stat command

Activity Monitor

Activity Monitor is the easiest method to determine RAM usage.  It’s part of Mac OS X and is readily available under the Utilities folder in the Applications folder:

The System Memory tab specifically displays details.  You can see that my Mac Book Pro has a total of 3 GB of physical RAM.  The “Free” category is showing a meager 48 MB of RAM.  Is this a problem?  (My system is perpetually near this mark, no matter how many or few applications are opened and running)

What do these various categories mean? The Developer Library article titled Memory Management in Mac OS X sheds some light on this topic.

  • Free – This is memory that is not in use for any purpose and is readily available.
  • Wired – Memory that cannot be paged out to disk, as it is vital to the system.  Also known as resident memory.  This memory mostly consists of OS kernel code and data structures such as drivers, per process and thread structures, etc.
  • Active – Memory currently used by applications, frameworks, etc.  All of the per process real memory usage listed in the Activity Monitor screen shot represents Active memory.
  • Inactive – Memory pages containing data that has not been utilized recently.
  • Used – This is simply a summation of the Wired, Active, and Inactive amounts.
  • VM Size – This is the aggregate of all virtual address space allocated to processes.  This number doesn’t correspond to physical memory used or even disk space, it is a fictitious number.   The 2.95 GB of physical RAM and 5.47 GB of Swap Used are the only resources actually in use for this VM Size.

The Active and Inactive categories are quite sizable, at 1.88 GB and  847 MB respectively in the screen shot above.  How can we determine how much of this memory is in use for page cache?   We can run the purge command (Note that this will stress the system such that it will freeze for up to several seconds as it flushes memory to disk).  The purge command will force a flush of the disk cache.  We can then view the resulting decrease in Active and Inactive memory, and increase in Free memory.  In my case, a purge resulted in an increase of almost 600 MB of free memory.   Note that this number will slowly decrease again as the OS uses the memory to re-populate the disk cache.

vm_stat command

From a terminal window, the vm_stat command can be used to gather similar statistics:

The vm_stat command displays information in units of pages, which as shown above are 4,096 bytes.   The 66,084 pages of free memory correspond to approx 260 MB of free memory, which is the amount reported by Activity Monitor as I am typing this.   Further explanation for vm_stat can be gleaned from the corresponding man pages.

Disk Cache in action

The easiest way to see the disk cache in action is to copy a large file.  Here we copy a 4.2 GB gzipped ISO image file.  Free memory plummets from 146 MB to 1.5 MB as the file contents are buffered in memory.  Also notice that while Active pages have changed little, Inactive has changed a lot.  There is 94 MB of Inactive memory after the copy completes, up from 25 MB.  Therefore the Free memory situation isn’t quite as dire as it appeared.

NOTE: Still exploring these results, seems inaccurate.  the VM_STAT output is not aligning with what activity monitor is showing.

Summary

To accurately measure available memory, keep in mind both Free memory and Inactive memory are readily available to the operating system.  Further, some of that which is marked as Active may also be available, as some portion of Active seems to include the disk cache.