Resident Memory for Process in Big Sur

Hi,

I've been trying to benchmark certain algorithms on my M1 MBP with Big Sur. I've been using the open source memory_profiler application (https://github.com/pythonprofilers/memory_profiler) which on my machine uses the psutil library (https://github.com/giampaolo/psutil) under the hood.

The psutil library uses an API for computing the resident memory of a process. Unfortunately, on Big Sur, the number returned by the API is far too large (Activity Monitor seems to provide an accurate measurement).

The API used by psutil is described by https://opensource.apple.com/source/xnu/xnu-2050.7.9/bsd/kern/proc_info.c

Under the hood, it calls proc_pidinfo.

I've also tried using a mach API which seems to give an answer that is too low.

Code Block #include "mach/mach.h"
vm_size_t usedMemory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = TASK_BASIC_INFO_COUNT;
kern_return_t kerr = task_info(task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
return (kerr == KERN_SUCCESS) ? info.resident_size : 0; // size in bytes
}


Others have claimed that the virtual memory number is wrong too.

What is the correct way to query the instantaneous memory usage of a process in C/C++?

Thanks!



Resident Memory for Process in Big Sur
 
 
Q