Fatest way to get available memory on Linux



  • Hi,
    I am developping a video recording program but it sometime exhaust the system's memory.
    I've looked on forums and the simplest solution to get the available memory was this one:

    QProcess qp;
    while (!stopCapture) {
            /* Gets captured image from camera and stores it in a vector */
    
            qp.start("awk", QStringList() << "/MemAvailable/ {print $2}" << "/proc/meminfo");
            qp.waitForFinished();
            QString qs_AvailableMemory = qp.readAllStandardOutput();
            if (qs_AvailableMemory.toLong() / 1024 < 128)
                    break;
    }
    qp.close();
    

    The problem is that this code takes about 10 milliseconds to execute, which causes dropped frames.
    Is there a faster way to get the system's available memory?


  • Qt Champions 2017

    @bs55

    I guess there might be better ways to achive your final solution video recording (temporary files, e.g.).

    But for your question: a faster way to read /proc/meminfo (which is just a file on a pseuso filesystem) may be using QFile. The fastest way, however would be using API: http://man7.org/linux/man-pages/man2/sysinfo.2.html

    Please note, that both solutions are not portable code (well, sysinfo may work on other Unixes and maybe even on macOS).



  • @aha_1980 Thank you for your answer.
    Unfortunatly saving the file is not an option as it takes about 25 ms to save an image so I cannot do it while recording without dropping at least 4 frames...
    Parsing the /proc/meminfo is just slightly faster than using the AWK command but not fast enough.
    I will look into this sysinfo API.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.