以编程方式获取 OS X 上安装的 RAM 量
我正在一台安装了 8 GB 内存的机器上工作,我试图以编程方式确定机器中安装了多少内存。我已经尝试使用 sysctlbyname() 来获取安装的内存量,但它似乎仅限于返回有符号的 32 位整数。
uint64_t total = 0;
size_t size = sizeof(total);
if( !sysctlbyname("hw.physmem", &total, &size, NULL, 0) )
m_totalMemory = total;
上面的代码,无论传递给sysctlbyname什么类型,总变量中总是返回2147483648。我一直在通过 IOKit 和 IORegistryExplorer 寻找另一种确定已安装内存的途径,但到目前为止还没有找到任何结果。我在 IORegistryExplorer 中找到了 IODeviceTree:/memory,但其中没有大小字段。我在 IO 注册表中也没有找到任何其他内容。有没有办法通过 IOKit 访问此信息,或者让 sysctlbyname 返回超过 32 位有符号整数?
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. 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.
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. I've been searching through IOKit and IORegistryExplorer for another route of determining installed memory, but have come up with nothing so far. I've found IODeviceTree:/memory in IORegistryExplorer, but there's no field in there for size. I'm not finding anything anywhere else in the IO Registry either. Is there a way to access this information via IOKit, or a way to make sysctlbyname return more than a 32-bit signed integer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
sysctl()
并查询HW_MEMSIZE
。这会以 64 位整数形式返回内存大小,而不是默认的 32 位整数。手册页给出了细节。
You can use
sysctl()
and queryHW_MEMSIZE
.This returns the memory size as a 64-bit integer, instead of the default 32-bit integer.The man page gives the details.
最简单的方法:
The easy way: