QtSerialPort and memory usage



  • Hi,

    I have this following code reading the serial port on Windows.

    As the application keeps reading the serial port byte after byte,
    I do see that the memory usage keeps on going up. But if I do
    comment out the read(), the memory usage is stable and constant.

    Is it something that I am missing or doing incorrectly ?

    Thanks,

    #include <QtSerialPort/QSerialPort>
    #include <QCoreApplication>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QSerialPort port;
    char dt;

    port.setPortName("COM13");
    if (!port.open(QIODevice::ReadWrite)) {
    	qDebug() << "Open" << port.portName() << "failed";
    	return -1;
    }
    qDebug() << port.portName() << "Open";
    port.setBaudRate(QSerialPort::Baud9600);	// Baud rate
    port.setDataBits(QSerialPort::Data8);		// Data bits
    port.setParity(QSerialPort::NoParity);		// Parity
    port.setStopBits(QSerialPort::OneStop);		// Stop bits
    port.setFlowControl(QSerialPort::NoFlowControl);// Flow control
    
    port.setReadBufferSize(1);
    while (1) {
    	if (port.waitForReadyRead(10)) {
    		port.read(&dt, 1);
    	}
    }
    return a.exec();
    

    }


  • Qt Champions 2016

    +1 for running able code.
    Win 7, 64. mingw. Qt 5.5
    memory seems stable.
    how long to watch ?



  • @mrjj

    I am running
    Win 7 SP1 32bit Version 6.1.7601
    QT 5.5.0,
    QT creator: 3.4.2 revision b57ac109a2
    MinGW 2013072200

    The memory usage, seen as with Task Manager
    is as follows:

    starts at about : ~2.4M
    @ about 10Sec : ~32M
    @ 60Sec : ~64M
    @ 120Sec : ~96M
    @ about 300Sec : ~227M

    I dont know, just a thought, as though each read() is not
    freeing up read memory within the library. Is that possible situation ?
    But then, lot of other people should also be facing the same situation,
    but I guess that isnt the scenario.

    Thanks


  • Qt Champions 2016

    @codeaway
    Well bugs are always possible.
    Also tried at work. After 25 sec, still have same Memory.
    So I cannot reproduce it easy.
    Is it a real comport or a virtual one ?
    both tested here are virtual.
    (com1.)
    I wonder if it could be your driver .
    can you reproduce it on other system ?
    update:
    tried on system with real comport. still stable mem.
    sorry.



  • @mrjj

    I am using a BAFO USB to serial converter
    as seen here:
    http://www.dx.com/p/bafo-usb-to-rs232-converter-5947#.VhTCIuyqpHw

    Trying to eliminate some unknown possible situations:
    Assuming for a moment that there is a hardware bug with the
    USB to serial converter hardware/driver

    The same serial hardware is connected to the same converter and is read
    with Terra Term (Version 4.76)

    It's memory usage (Private Working set) remains stable at 8,668K reading
    the same data. I just wonder why it is showing stable memory usage.

    http://postimg.org/image/a2qb10ev1/

    I wondered, how the other QtSerialExamples behaved;
    Tried with the simple terminal example
    D:\Qt\Qt5.5.0\5.5\Src\qtserialport\examples\serialport\terminal\release>

    Even terminal shows a memory usage increment, much like the code snippet
    that's under scrutiny. I wonder, how Terra Term is able to cope with a
    consistent memory usage

    Updated the original code to print out the memory usage.
    Updated code output screencap shows the usage memory
    incrementing:
    http://postimg.org/image/cfofnghnl/

    For sake of completeness, including the updated code in here

    #include <windows.h>
    #include "psapi.h"

    #include <QtSerialPort/QSerialPort>
    #include <QCoreApplication>
    #include <QDebug>
    #include <QDateTime>

    int main(int argc, char *argv[])
    {
    PROCESS_MEMORY_COUNTERS_EX pmc;
    QCoreApplication a(argc, argv);
    QSerialPort port;
    char dt;

    port.setPortName("COM13");
    if (!port.open(QIODevice::ReadWrite)) {
    	qDebug() << "Open" << port.portName() << "failed";
    	return -1;
    }
    
    qDebug() << port.portName()
    	 << "Open @" << QDateTime::currentMSecsSinceEpoch();
    
    port.setBaudRate(QSerialPort::Baud9600);	// Baud rate
    port.setDataBits(QSerialPort::Data8);		// Data bits
    port.setParity(QSerialPort::NoParity);		// Parity
    port.setStopBits(QSerialPort::OneStop);		// Stop bits
    port.setFlowControl(QSerialPort::NoFlowControl);// Flow control
    
    port.setReadBufferSize(1);
    while (1) {
    	if (port.waitForReadyRead(10)) {
    		port.read(&dt, 1);
    		GetProcessMemoryInfo(GetCurrentProcess(),
    				    (PROCESS_MEMORY_COUNTERS*)&pmc,
    				     sizeof(pmc));
    
    		qDebug() << "Read @" << QTime::currentTime()
    			 << "Used:" << pmc.PrivateUsage;
    	}
    }
    return a.exec();
    

    }


  • Qt Champions 2016

    Hmm if tera works with same usb serial then it cant be the driver.
    Im also using serial over usb.

    Tried to run updated code. just to ebe sure.
    get link error
    undefined reference to `GetProcessMemoryInfo@12'

    What should i put in .pro to run ?
    something with -lpsapi.
    Mostly on linux so not sure with syntax



  • @mrjj

    Yes.

    Sorry, missed the .pro file. inlined it

    #-------------------------------------------------

    Project created by QtCreator 2015-10-06T21:35:43

    #-------------------------------------------------

    win32:LIBS += -lpsapi

    QT += core serialport

    QT -= gui

    TARGET = Serial
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp


  • Qt Champions 2016

    works.
    Still stable.
    Do you have input on the serial ?
    I will connect something that sends data. just to check.
    How do you make it open console also ?
    Here its just a process.
    Update:
    ignore last question. just saw its QDebug
    Also, with input , I do see mem raising.
    Testing more


  • Qt Champions 2016

    ok,
    slowly from 12k to 17k. over time when it has input.
    Also the pmc changes.
    So yes. I see the same.
    Will leave it running for some time to check if it stops.



  • @mrjj

    Yes, I do have a microcontroller reading values and outputting to serial port
    continuously.

    Actually, the application I created is a console application. The info is output
    with QDebug additionally.

    Nevertheless, you can choose to output with console without QDebug too.

    CONFIG += console

    in the .pro file

    Create a output stream in the actual code

    QTextStream out(stdout);

    and output whatever required

    out << "Something" << endl;


  • Qt Champions 2016

    @codeaway
    Ok i killed it at 25K.
    Since we just reading a char, it cant be in the us that leaks.

    I must agree it seems odd.





  • @kuzulis

    I downloaded the x86 build, as I am running a 32 bit OS.
    How should I configure com0com ?


  • Qt Champions 2016

    @kuzulis
    just as a virtual port or as a loop ?



  • How should I configure com0com ?

    Just create a pair with the "use port class" and "eneble buffer overrun" (via setupg.exe ui)

    just as a virtual port or as a loop ?

    as a two virtual ports. . e.g. COM1 as receiver for qserialport and COM2 as sender for other software (e.g. I have used Termite app to send).

    UPD:

    Yes, I too can reproduce this memory leak..



  • @kuzulis

    Yeah, reproduced it with com0com and termite.
    Attached link to screencap

    http://postimg.org/image/mku4ege29/

    After a detailed analysis, it looks as though all QtSerialPort examples
    also face this issue.


  • Qt Champions 2016

    @codeaway
    Yep got ever increasing mem use also with com0com.
    If time permits I try the examples too.



  • Probably it is a bug of QWinOverlappedIoNotifier : https://bugreports.qt.io/browse/QTBUG-48653
    Let's wait for more info about..


Log in to reply
 

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