在OSX上以编程方式确定物理内存大小 [英] Determine physical mem size programmatically on OSX

查看:94
本文介绍了在OSX上以编程方式确定物理内存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图找出在运行Mac OS X的计算机中安装了多少物理内存.我们找到了BSD函数sysctl().问题在于此函数要返回32位值,但是某些Mac能够寻址最大32 GB的地址,而该GB不能容纳32位值. (实际上,甚至4 GB也无法容纳32位值.)OS X(10.4或更高版本)上是否有其他API可以提供此信息?

We're trying to find out how much physical memory is installed in a machine running Mac OS X. We've found the BSD function sysctl(). The problem is this function wants to return a 32 bit value but some Macs are able to address up to 32 GB which will not fit in a 32 bit value. (Actually even 4 GB won't fit in a 32 bit value.) Is there another API available on OS X (10.4 or later) that will give us this info?

推荐答案

答案是使用sysctl来获取hw.memsize,如先前答案中所建议的那样.这是执行此操作的实际代码.

The answer is to use sysctl to get hw.memsize as was suggested in a previous answer. Here's the actual code for doing that.

#include <sys/types.h>
#include <sys/sysctl.h>

...

    int mib[2];
    int64_t physical_memory;
    size_t length;

    // Get the Physical memory size
    mib[0] = CTL_HW;
    mib[1] = HW_MEMSIZE;
    length = sizeof(int64_t);
    sysctl(mib, 2, &physical_memory, &length, NULL, 0);

这篇关于在OSX上以编程方式确定物理内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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