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. Why the process memory continouly increase
Forum Updated to NodeBB v4.3 + New Features

Why the process memory continouly increase

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    BernardZhu
    wrote on last edited by
    #1

    It is a tcp client, just receive what server sends, i found the process memory continusly increasing
    I tested with QT6.8.1 on Linux and Windows. I am new to QT, not sure what happend

    Screenshot 2025-01-04 003007.png

    Screenshot 2025-01-04 003139.png

    #include <QCoreApplication>
    
    #include <QDebug>
    #include <QTcpSocket>
    
    int main(int argc, char* argv[])
    {
    	QCoreApplication a(argc, argv);
    
    	auto s = new QTcpSocket(0);
    	s->connectToHost("127.0.0.1", 17725);
    	s->waitForConnected(-1);
    	int i = 0;
    	QObject::connect(s, &QTcpSocket::readyRead, [&]() {
    		s->read(12);
    		return 0;
    		});
    
    	return a.exec();
    }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      BernardZhu
      wrote on last edited by
      #2

      Looks like the QTcpSocket will read the incoming data in background and save it to a buffer, But in the readyRead slot, the example program just read 12 bytes, so there are more and more data left in the QTcpSocket maintained buffer, that's why it continues memeory increas. If i change s->read(10) to s->readAll(), the memory usage keeps still

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BernardZhu
        wrote on last edited by
        #3

        From QAbstractSocket document:

        The readyRead() signal is emitted every time a new chunk of data has arrived. bytesAvailable() then returns the number of bytes that are available for reading. Typically, you would connect the readyRead() signal to a slot and read all available data there. If you don't read all the data at once, the remaining data will still be available later, and any new incoming data will be appended to QAbstractSocket's internal read buffer. To limit the size of the read buffer, call setReadBufferSize().

        Pl45m4P 1 Reply Last reply
        2
        • B BernardZhu

          From QAbstractSocket document:

          The readyRead() signal is emitted every time a new chunk of data has arrived. bytesAvailable() then returns the number of bytes that are available for reading. Typically, you would connect the readyRead() signal to a slot and read all available data there. If you don't read all the data at once, the remaining data will still be available later, and any new incoming data will be appended to QAbstractSocket's internal read buffer. To limit the size of the read buffer, call setReadBufferSize().

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @BernardZhu said in Why the process memory continouly increase:

          If you don't read all the data at once, the remaining data will still be available later, and any new incoming data will be appended to QAbstractSocket's internal read buffer. To limit the size of the read buffer, call setReadBufferSize().

          Good to hear that you've figured it out.
          You can mark your own answer as the solution :)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          • Login

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