How can I obtain this information:
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?
To specify global settings for the Performance Analyzer, select Edit > Preferences > Analyzer > CPU Usage. For each run configuration, you can also use specialized settings. Select Projects > Run, and then select Details next to Performance Analyzer Settings.
Unfortunately, there is nothing built into Qt for this. You must do this per-platform.
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();
Much better on POSIX OSes (Linux, Solaris, perhaps latest MacOS...) :
So much treasures on POSIX computers not available on Windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With