mac os下进程使用的内存x [英] Memory used by a process under mac os x

查看:381
本文介绍了mac os下进程使用的内存x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个PID,如何获取进程当前使用的内存?具体来说我正在寻找:

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


  • 进程使用的私有物理内存(RAM)

  • 进程使用的交换空间

但我对映射文件和共享内存不感兴趣。总之,我想确定通过终止PID来释放多少内存(RAM和交换)。

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.

推荐答案

可以使用Mach的task_info调用来查找这些信息。下面是适用于OS X v10.9的代码,它获取当前进程的虚拟进程大小:

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.

这篇关于mac os下进程使用的内存x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆