Get memory usage for pid

Hello,

How do I get physical memory usage and cpu percent of a running process; using the process id? I was looking at kinfo_proc but I dont see memory or cpu.

Something similar to the output of activity monitor but for a single PID.



Thanks!

Replies

proc_pidinfo

The concept of physical memory usage is a fuzzy one. My recommendation is that you use the footprint value, as discussed in this post.

For CPU usage, the libproc approach suggested by john daniel is a good place to start.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I find pti_resident_size to be a very reliable and consistent way to measure memory usage. It seems to match what ps and top display. That is important for me when I'm running out of the sandbox and ps/top aren't available. Even if it isn't perfectly accurate, it is relatively accurate in terms of being an Apples-to-Apple's comparison.


It is true that I'll never be able to match what Activity Monitor displays in any of its 6 memory usage columns, but that isn't very important. Apple's memory usage and footprint calculations are not a public API, so any attempt to reverse engineer what Apple is doing is virtually guaranteed to fail.