Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting memory information with Qt

Tags:

memory

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?

like image 356
mateusz_s Avatar asked Nov 14 '11 13:11

mateusz_s


People also ask

How do you use Performance Analyzer in Qt?

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.


2 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(); 
like image 59
Dave Mateer Avatar answered Oct 12 '22 11:10

Dave Mateer


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.

like image 45
GalaxySemi Avatar answered Oct 12 '22 09:10

GalaxySemi