Getting memory information with Qt

How can I obtain this information:

  • Total Memory
  • Free Memory
  • Memory used by current running application ?

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?

Answers


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...) :

  • getrusage(...) secially look at ru_maxrss.
  • getrlimit(...) but I did not find any usefull info into.
  • sysconf(...) : _SC_PAGESIZE, _SC_PHYS_PAGES, _SC_AVPHYS_PAGES
  • sysinfo(...) : totalram, freeram, sharedram, totalswap,...

So much treasures on POSIX computers not available on Windows.


This is currently not possible in Qt. You would need to ifdef the different OS memory calls.


Need Your Help

How to know when the MPMoviePlayerController has been paused in iPhone?

iphone notifications mpmovieplayercontroller

I want to add an overlay view for my video when the video is paused by the user. Is there any way to get the pause notification from MPMoviePlayerController?

Define click event for UISegmentedControl

ios cocoa-touch uisegmentedcontrol

I have added a UISegmentedControl in my application. None of the buttons are selected in normal state. I want to implement a button click event when the first segment is selected, and another event