2
votes

I'd like to monitorize how RAM memory I used for may Qt application; so, I thought about something to put inside the code. I tried the following:

QProcess p;
p.start("ps -A");
p.waitForFinished();
QByteArray RamMem =p.readAllStandardOutput();
p.close();
quint16 pidcounter = 0;
QString pidString(RamMem);
QStringList RamMemSplit = pidString.split('\n');
quint16 RamMemSplitcounter = RamMemSplit.count();
while(pidcounter< RamMemSplitcounter)
{
    if (RamMemSplit[pidcounter].contains(MyApp))
    {
        splitsplit = RamMemSplit[pidcounter].split(" ");
        qDebug() << "Process:"<< splitsplit[10]<< "pid:"<< splitsplit[0];
    }
    pidcounter++;
}

In this way, I save name of running process associated to its pid; Now, I'd like to apply another process ("pmax -x mypid), so I can obtain the RAM amount of my app: How can I perform this? I read a QProcess requires a QStringList argument; in my case, I have only one parameter to use as argument and it's no so clear how to correctly set tyhe QProcess. The following is my idea, connected to the previous part:

QStringList listprova(splitsplit[0]);
QProcess pr;
pr.start("pmap -x", listprova);
pr.waitForFinished();
QByteArray pmapResult = pr.readAllStandardOutput();
pr.close();
QString  pmapString(pmapResult);
QStringList pmapSplit = pmapString.split('\n');
quint8 pmapCounter = pmapSplit.count();
qDebug() << pmapSplit[pmapCounter]; // last line of "pmap -x [pid]" console command
1

1 Answers

3
votes

You have 2 arguments, -x and the pid.

QStringList arguments;
arguments << "-x" << splitsplit[0];
QProcess pr;
pr.start("pmap", arguments);