MacOS – How to get the amount of ‘Compressed Memory’, per process (from Terminal)

activity-monitormacosmemoryterminalvirtual-memory

How can I use ps, or some variation thereof… to get a value that roughly corresponds to the "Compressed Memory" value Apple reports in Activity Monitor.app?

enter image description here

username 52036 100.1 24.1 8026832 3035512 ?? Rs ...com.apple.WebKit.WebContent

Ok, so it appears ps aux tells us…

  • 3035512, aka 2.83 Gb, which corresponds to "Private Memory"
  • 8026832, aka 7.48 Gb which doesn't seem to correspond to anything. (Although it possibly could be ("Memory" 3.89 + "Private Memory" 2.84 + "Compressed Memory" 1.05)?

Is there any way "normal" way to access this metric, or is it just some "magic" value Apple has come up with?

Best Answer

There's nothing magic about compressed memory. It's something that was added to 10.9 as one last step before the system swaps RAM out to storage. According to John Siracusa, this serves three major functions:

Memory compression is a triple play for Mavericks. It’s a performance win; compressing and decompressing data in RAM is much faster than reading from and writing to disk, even an SSD. It’s an energy win; the less time spent moving data between RAM and disk, the more time the system can spend in its idle state. And finally, it’s a capability win; Mavericks can handle much more demanding workloads than previous versions of OS X before crying uncle.

As to getting at the details from the command line - that may be elusive. The overall compression numbers are easy with a pair of tools:

memory_pressure
vm_stat

I don't think any invocation of ps will do the trick, but maybe someone can correct that. You can see the implementation details on this thread: vm_compressor_mode (vm.compressor_mode) values for enabled compressed memory in OS X

The best I can get you is to look at all "swapped and/or compressed" memory using the vmmap command. Basically, before swapping, the memory regions marked for swapping are compressed. If that saves enough space to alleviate the swap algorithm, no swap happens. If not, the compressed regions are swapped to storage.

vmmap -swapped [PID]

You'd have to handle the addition of the regions and I'm not sure if the activity monitor reports all swapped and compressed in the column you mentioned but that's something you might be able to figure out by inspection and/or using Instruments from Xcode to correlate what you see from the command line with activity monitor for some processes.