Search code examples
macosmemory-managementcpu-usagemacos-sierradarwin

Calculating Per Process Memory Usage on MacOS


I am currently working on a small side project which I would like to add some statistics to regarding CPU usage / Memory Usage / Opened FDs / etc. but on a per process level.

I have the CPU usage working correctly, however the issue I am running into is with the memory. I cannot find any APIs for retrieving or calculating the memory usage similar to how 'Activity Monitor' does it in the process info section.

What APIs are available to be able to calculate the 'Real/Virtual/Shared/Private' memory usages? I have used the host_statistics() however this only gives me the basic VM info from what I can tell. I have also looked into task_info() but this too does not appear to have the memory break down I am looking for.


Solution

  • Apple makes the source code for the top utility available. That should help you work out how to access all the process information it displays, at least. This does include a bunch of different memory statistics.