简体   繁体   English

以编程方式获取OS X上安装的RAM量

[英]Programmatically get amount of RAM installed on OS X

I'm working on a machine that has 8 gigs of memory installed and I'm trying to programmatically determine how much memory is installed in the machine. 我正在安装一台安装了8 GB内存的机器,我正在尝试以编程方式确定机器中安装了多少内存。 I've already attempted using sysctlbyname() to get the amount of memory installed, however it seems to be limited to returning a signed 32 bit integer. 我已经尝试使用sysctlbyname()来获取安装的内存量,但它似乎仅限于返回一个带符号的32位整数。

uint64_t total = 0;
size_t size = sizeof(total);
if( !sysctlbyname("hw.physmem", &total, &size, NULL, 0) )
     m_totalMemory = total;

The above code, no matter what type is passed to sysctlbyname, always returns 2147483648 in the total variable. 上面的代码,无论传递给sysctlbyname的类型是什么,总是在total变量中返回2147483648。 I've been searching through IOKit and IORegistryExplorer for another route of determining installed memory, but have come up with nothing so far. 我一直在通过IOKit和IORegistryExplorer搜索确定已安装内存的另一条路径,但到目前为止还没有提出任何建议。 I've found IODeviceTree:/memory in IORegistryExplorer, but there's no field in there for size. 我在IORegistryExplorer中找到了IODeviceTree:/ memory,但是那里没有大小的字段。 I'm not finding anything anywhere else in the IO Registry either. 我也没有在IO注册表中找到任何其他地方。 Is there a way to access this information via IOKit, or a way to make sysctlbyname return more than a 32-bit signed integer? 有没有办法通过IOKit访问此信息,或者使sysctlbyname返回多于32位有符号整数的方法?

You can use sysctl() and query HW_MEMSIZE .This returns the memory size as a 64-bit integer, instead of the default 32-bit integer. 您可以使用sysctl()并查询HW_MEMSIZE 。这会将内存大小返回为64位整数,而不是默认的32位整数。

The man page gives the details. 手册页提供了详细信息。

简单的方法:

[[NSProcessInfo processInfo] physicalMemory]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM