简体   繁体   中英

Get memory usage of a process from C++

I am trying to determine how much memory the current process is using, on Mac OS X. To be specific, how does top calculate the value displayed in the MEM column for each process? Activity Monitor displays the same value in the Memory column for each process on the Memory tab.

There are a few pages on the web (like this answer ) suggesting the following:

struct task_basic_info tinfo;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
task_info(
    mach_task_self(), TASK_BASIC_INFO,
    (task_info_t) &tinfo, &count);
// memory usage is in tinfo.resident_size;

But the returned value is off by almost a factor 2 (eg top shows 64 MB, while this code reports 105MB). How do top and Activity Monitor find the memory usage of a process?

Based on my OSX app, so maybe I'm wrong, it sounds like. Top/XCode tools report the memory allocated by the "new/malloc/?" calls, eg: the allocations performed by the code itself. Instead the task info includes the memory used by the OS to load the executable + all the allocations you made. In my case I've got a delta of 75MB that's around the size of my debug build (resources excluded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM