Memory used by a process under mac os x

放肆的年华 提交于 2019-12-21 04:25:35

问题


Given a PID, how can I get the memory currently used by a process ? Specifically I am looking for:

  • The private physical memory (RAM) used by the process
  • The swap space used by the process

But I am not interested in mapped files and shared memory. In short, I would like to determine how much memory (RAM and swap) would be freed by terminating the PID.


回答1:


You can use Mach's task_info call to find this information. Here is code which works on OS X v10.9, and which gets the virtual process size of the current process:

#include <mach/mach.h>
#include <mach/message.h>  // for mach_msg_type_number_t
#include <mach/kern_return.h>  // for kern_return_t
#include <mach/task_info.h>
#include <stdio.h>

int main(void) {
  kern_return_t error;
  mach_msg_type_number_t outCount;
  mach_task_basic_info_data_t taskinfo;

  taskinfo.virtual_size = 0;
  outCount = MACH_TASK_BASIC_INFO_COUNT;
  error = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskinfo, &outCount);
  if (error == KERN_SUCCESS) {
    // type is mach_vm_size_t
    printf("vsize = %llu\n", (unsigned long long)taskinfo.virtual_size);
    return 0;
  } else {
    printf("error %d\n", (int)error);
    return 1;
  }
}

I think that this excludes shared memory segments, but I'm not sure.




回答2:


Would this be useful? You can use the ps command with various options to get at least some of those things:

ps -o rss -o vsz -o pid

will give you the resident set size, the virtual size, and the process ID. I see from the man page that -o paddr gives the swap address, but I don't see which option gives you the swap size.



来源:https://stackoverflow.com/questions/18389581/memory-used-by-a-process-under-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!