3 Replies
      Latest reply on Jul 16, 2019 5:44 PM by john daniel
      cheizer Level 1 Level 1 (0 points)


        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.




        • Re: Get memory usage for pid
          john daniel Level 3 Level 3 (450 points)


          • Re: Get memory usage for pid
            eskimo Apple Staff Apple Staff (12,075 points)

            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"

              • Re: Get memory usage for pid
                john daniel Level 3 Level 3 (450 points)

                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.