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

查看:1354
本文介绍了使用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天全站免登陆