检索在Mac上RAM的信息? [英] Retrieve RAM info on a Mac?

查看:167
本文介绍了检索在Mac上RAM的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检索的系统中,目前正在使用的总的RAM的RAM present的总量,所以可以计算的百分比。这是类似的:找回在MacOS系统的信息X

I need to retrieve the total amount of RAM present in a system and the total RAM currently being used, so I can calculate a percentage. This is similar to: Retrieve system information on MacOS X?

不过,在这个问题的最佳答案建议如何通过阅读来获得RAM:

However, in that question the best answer suggests how to get RAM by reading from:

/usr/bin/vm_stat

由于我的程序的性质,我发现我不是不能从文件中读取 - 我需要一个方法,将提供给我的信息的RAM不只是打开一个文件,并从中读取。我寻找的东西做函数调用。事情是这样的preferably: getTotalRam() getRamInUse()

我显然不希望它是简单的,但我一直在寻找比从文件中读取其他的解决方案。

I obviously do not expect it to be that simple but I was looking for a solution other than reading from a file.

我运行Mac OS X雪豹,但将preferably得到一个解决方案,将在所有当前Mac OS X平台上(即狮子)。工作

I am running Mac OS X Snow Leopard, but would preferably get a solution that would work across all current Mac OS X Platforms (i.e. Lion).

解决方案可以在C ++,C或对象 - 不过C ++就在我的情况下,最佳的解决方案,所以如果有可能,请尽量提供它在C ++中。

Solutions can be in C++, C or Obj-C, however C++ would the best possible solution in my case so if possible please try to provide it in C++.

推荐答案

你不应该从/ usr / bin中/ vm_stat阅读,而你应该运行它;它是一个程序。看看前四行输出

You're not supposed to read from /usr/bin/vm_stat, rather you're supposed to run it; it is a program. Look at the first four lines of output

Pages free:                  1880145.
Pages active:                  49962.
Pages inactive:                43609.
Pages wired down:             123353.

添加数字在右栏中,多由系统页面大小(如为getpagesize()返回),你会得到以字节为单位的物理内存在系统中的总量。

Add the numbers in the right column and multiple by the system page size (as returned by getpagesize()) and you get the total amount of physical memory in the system in bytes.

vm_stat不会是setuid在Mac OS,所以我假定有一个非特权API某处访问此信息和vm_stat在使用它。但我不知道该接口是什么。

vm_stat isn't setuid on Mac OS, so I assume there is a non-privileged API somewhere to access this information and that vm_stat is using it. But I don't know what that interface is.

这篇关于检索在Mac上RAM的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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