34

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
mateusz_s
  • 401
  • 1
  • 5
  • 11
  • http://stackoverflow.com/questions/27201876/memory-management-issue-with-deleting-qquickview-in-qt5-3mingw32/27203897#27203897 – dtech Jan 23 '16 at 00:14

3 Answers3

55

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();
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
  • What about Android and iOS now that Qt supports them? – sashoalm Dec 11 '14 at 15:46
  • 2
    Thank you for this (a few years later). One addendum to this though, on Mac, sysctl actually wants hw.memsize, not hw.physmem. The output comes in like "hw.memsize: nnnnnnn" and it comes back in bytes so if you want MB you still need to do the / (1024 * 1024) after converting the string to an int. – Gabe Weiss Aug 03 '15 at 16:48
  • 1
    Shouldn't the linux example, be: QString system_info = p.readAllStandardOuptut, not memory ? – SPlatten Jul 03 '17 at 13:56
  • @sashoalm, since Android uses Linux, above answer for Linux, works for android too. not sure about iOS –  Mar 09 '20 at 19:55
  • Header includes would be really helpful. – plasmacel May 19 '20 at 09:49
3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
GalaxySemi
  • 51
  • 1
0

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

Phil Hannent
  • 12,047
  • 17
  • 71
  • 118