Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtSerialPort and memory usage
QtWS25 Last Chance

QtSerialPort and memory usage

Scheduled Pinned Locked Moved General and Desktop
serialport
18 Posts 3 Posters 7.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    codeaway
    wrote on 6 Oct 2015, 16:16 last edited by
    #1

    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();
    

    }

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Oct 2015, 18:33 last edited by
      #2

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

      C 1 Reply Last reply 7 Oct 2015, 02:02
      0
      • M mrjj
        6 Oct 2015, 18:33

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

        C Offline
        C Offline
        codeaway
        wrote on 7 Oct 2015, 02:02 last edited by
        #3

        @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

        M 1 Reply Last reply 7 Oct 2015, 06:23
        0
        • C codeaway
          7 Oct 2015, 02:02

          @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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 7 Oct 2015, 06:23 last edited by mrjj 10 Jul 2015, 06:36
          #4

          @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.

          C 1 Reply Last reply 7 Oct 2015, 07:21
          0
          • M mrjj
            7 Oct 2015, 06:23

            @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.

            C Offline
            C Offline
            codeaway
            wrote on 7 Oct 2015, 07:21 last edited by
            #5

            @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();
            

            }

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Oct 2015, 07:30 last edited by
              #6

              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

              C 1 Reply Last reply 7 Oct 2015, 07:32
              0
              • M mrjj
                7 Oct 2015, 07:30

                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

                C Offline
                C Offline
                codeaway
                wrote on 7 Oct 2015, 07:32 last edited by
                #7

                @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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 7 Oct 2015, 07:39 last edited by mrjj 10 Jul 2015, 07:42
                  #8

                  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

                  C 1 Reply Last reply 7 Oct 2015, 07:56
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 7 Oct 2015, 07:55 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • M mrjj
                      7 Oct 2015, 07:39

                      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

                      C Offline
                      C Offline
                      codeaway
                      wrote on 7 Oct 2015, 07:56 last edited by
                      #10

                      @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;

                      M 1 Reply Last reply 7 Oct 2015, 08:11
                      0
                      • C codeaway
                        7 Oct 2015, 07:56

                        @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;

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 7 Oct 2015, 08:11 last edited by
                        #11

                        @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.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kuzulis
                          Qt Champions 2020
                          wrote on 7 Oct 2015, 08:47 last edited by
                          #12

                          Can you please test same with the virtual com0com: http://sourceforge.net/projects/com0com/
                          A signed driver for x64: https://code.google.com/p/powersdr-iq/downloads/detail?name=setup_com0com_W7_x64_signed.exe&can=2&q=

                          C M 2 Replies Last reply 7 Oct 2015, 09:04
                          0
                          • K kuzulis
                            7 Oct 2015, 08:47

                            Can you please test same with the virtual com0com: http://sourceforge.net/projects/com0com/
                            A signed driver for x64: https://code.google.com/p/powersdr-iq/downloads/detail?name=setup_com0com_W7_x64_signed.exe&can=2&q=

                            C Offline
                            C Offline
                            codeaway
                            wrote on 7 Oct 2015, 09:04 last edited by
                            #13

                            @kuzulis

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

                            1 Reply Last reply
                            0
                            • K kuzulis
                              7 Oct 2015, 08:47

                              Can you please test same with the virtual com0com: http://sourceforge.net/projects/com0com/
                              A signed driver for x64: https://code.google.com/p/powersdr-iq/downloads/detail?name=setup_com0com_W7_x64_signed.exe&can=2&q=

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 7 Oct 2015, 09:06 last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                kuzulis
                                Qt Champions 2020
                                wrote on 7 Oct 2015, 11:54 last edited by
                                #15

                                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..

                                C 1 Reply Last reply 7 Oct 2015, 13:43
                                0
                                • K kuzulis
                                  7 Oct 2015, 11:54

                                  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..

                                  C Offline
                                  C Offline
                                  codeaway
                                  wrote on 7 Oct 2015, 13:43 last edited by
                                  #16

                                  @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.

                                  M 1 Reply Last reply 7 Oct 2015, 13:46
                                  0
                                  • C codeaway
                                    7 Oct 2015, 13:43

                                    @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.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 7 Oct 2015, 13:46 last edited by
                                    #17

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

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      kuzulis
                                      Qt Champions 2020
                                      wrote on 7 Oct 2015, 15:19 last edited by
                                      #18

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

                                      1 Reply Last reply
                                      1

                                      1/18

                                      6 Oct 2015, 16:16

                                      • Login

                                      • Login or register to search.
                                      1 out of 18
                                      • First post
                                        1/18
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved