使用 Qt 获取内存信息 [英] Getting memory information with Qt

查看:55
本文介绍了使用 Qt 获取内存信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得这些信息:

  • 总内存
  • 空闲内存
  • 当前运行的应用程序使用的内存?

我认为 Qt 应该有内存选项,这将与平台无关,但是我找不到.那么当我想制作一个显示内存状态的独立于平台的应用程序时,我该怎么做?

I think Qt should have memory options, that would be platform-independent, but I can't find it. So what can I do when I want to make a platform-independent application that shows memory state?

推荐答案

不幸的是,Qt 没有为此内置任何内容.您必须针对每个平台执行此操作.

Unfortunately, there is nothing built into Qt for this. You must do this per-platform.

这里有一些示例可以帮助您入门.就在上周,我不得不在我的一个应用程序中实现这一点.下面的代码仍在开发中;可能存在错误或泄漏,但它至少可以为您指明正确的方向.我只对总物理 RAM 感兴趣,但其他值以相同的方式可用.(可能除了当前应用程序正在使用的内存......不确定那个.)

Here are some samples to get you started. I had to implement this in one of my apps just last week. The code below is still very much in development; there may be errors or leaks, but it might at least point you in the correct direction. I was only interested in total physical RAM, but the other values are available in the same way. (Except perhaps memory in use by the current application ... not sure about that one.)

Windows (GlobalMemoryStatusEx)

MEMORYSTATUSEX memory_status;
ZeroMemory(&memory_status, sizeof(MEMORYSTATUSEX));
memory_status.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memory_status)) {
  system_info.append(
        QString("RAM: %1 MB")
        .arg(memory_status.ullTotalPhys / (1024 * 1024)));
} else {
  system_info.append("Unknown RAM");
}

Linux (/proc/meminfo)

QProcess p;
p.start("awk", QStringList() << "/MemTotal/ { print $2 }" << "/proc/meminfo");
p.waitForFinished();
QString memory = p.readAllStandardOutput();
system_info.append(QString("; RAM: %1 MB").arg(memory.toLong() / 1024));
p.close();

Mac (sysctl)

QProcess p;
p.start("sysctl", QStringList() << "kern.version" << "hw.physmem");
p.waitForFinished();
QString system_info = p.readAllStandardOutput();
p.close();

这篇关于使用 Qt 获取内存信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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