3

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I started taking a look at this, but it is not clear to me how they are calculating the private/shared memory values of a process.
Scratch that. Found the section of code that handles the Private/Shared calculations. Turns out you need to walk your virtual memory regions using mach_vm_region().
That sounds about right. I guess it's not 100% reliable as the process can be modifying its regions while you're walking them, but for the purposes of display stats it should suffice. (Sorry I'm not providing a full answer BTW but I don't know the details off by heart, and haven't really got the time to research it myself so I figured pointing you in the direction of where I'd start is better than nothing!)
No worries. It at least confirmed that I was on the right path of where I should be looking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.