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

发布于 2024-08-04 09:20:59 字数 528 浏览 3 评论 0原文

我正在一台安装了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-08-11 09:20:59

您可以使用 sysctl() 并查询 HW_MEMSIZE。这会以 64 位整数形式返回内存大小,而不是默认的 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.

The man page gives the details.

╰ゝ天使的微笑 2024-08-11 09:20:59

最简单的方法:

[[NSProcessInfo processInfo] physicalMemory]

The easy way:

[[NSProcessInfo processInfo] physicalMemory]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文