Why the process memory continouly increase
-
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#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(); }
-
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
-
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().
-
@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 :)