Wednesday, 15 August 2012

qt - Server memory growing -



qt - Server memory growing -

i developing download server in c++/qt. facing memory growing problem. here sharing sample server application demonstrate issue.

when client connected starts sending 10kb info chunks every second. when client disconnected, socket deleted.

#include <qcoreapplication> #include <qtnetwork> class client: public qobject { q_object public: client(qsslsocket *sock) { this->timer = new qtimer(this); this->timer->setinterval(1000); connect(this->timer, signal(timeout()), this, slot(senddata())); this->sock = sock; connect(sock, signal(disconnected()), this, signal(disconnected())); connect(sock, signal(encrypted()), this->timer, slot(start())); connect(sock, signal(readyread()), this, slot(readdata())); } ~client() { delete this->sock; } void start() { this->sock->startserverencryption(); } signals: void disconnected(); private slots: void senddata() { qdebug() << "sending info via socket: " << sock->socketdescriptor(); if (this->sock->bytestowrite()) return; qbytearray ba(10*1024, '1'); this->sock->write(ba); } void readdata() { this->sock->readall(); } private: qsslsocket *sock; qtimer *timer; }; // client class server: public qtcpserver { q_object public: server() { this->totalconnected = 0; this->instancecounter = 0; } protected: virtual void incomingconnection(int d); private: int totalconnected; int instancecounter; private slots: void handleclientdisconnected(); void handledestroyed(); }; // server void server::incomingconnection(int d) { qsslsocket *sock = new qsslsocket(this); if (!sock->setsocketdescriptor(d)) { delete sock; return; } ++this->instancecounter; qdebug() << "socket " << d << "connected, total: " << ++this->totalconnected; sock->setlocalcertificate(":/ssl/resources/my.crt"); sock->setprivatekey(":/ssl/resources/my.key", qssl::rsa, qssl::pem, "my.pass"); client *client = new client(sock); connect(client, signal(disconnected()), this, slot(handleclientdisconnected())); connect(client, signal(destroyed()), this, slot(handledestroyed())); client->start(); } void server::handleclientdisconnected() { qdebug() << "client disconnected, total: " << --this->totalconnected; sender()->deletelater(); } void server::handledestroyed() { qdebug() << "destroyed: " << --this->instancecounter; } int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); server server; if (server.listen(qhostaddress::any, 563)) qdebug() << "listen started"; else qdebug() << "listen failed"; homecoming a.exec(); } #include "main.moc"

there 2 questions it:

1) why memory continuously growing while downloading?

i testing 200-300 connections. reached 400 mb in few minutes , not going stop.

at client::senddata check this->sock->bytestowrite() know whether there waiting written. new info never added until written. info chunks have same size, can't has allocate more memory new piece of data.

2) why not memory returned when connections closed?

although memory used while downloading drops when clients disconnected, looks not of returned. after several tests of establishing 200-700 connections, reached 80 mb , stays @ level when there no clients @ (client objects reported destroyed, instance counter turns zero).

i reading difference between object deletion , memory deallocation. far understand scheme may reserve future needs (some kind of optimization). certainly must homecoming memory when else needs it. decided write little programme allocates big amount of memory (namely, array) see whether create scheme redeem memory used server. didn't. programme crashes, because there not plenty memory (it works fine when server started).

so looks there wrong. suspected leak, memory leak detectors didn't seem notice serious problems. visual leak detector reported there no memory leaks. , valgrind reported issues, pointing qt libs, , reading simply false alarms, side-effect of lower level libraries deallocating memory after valgrind. anyway, total size of reportedly lost info little comparing 80 mb.

memory allocators can configured homecoming blocks of unused memory system, true, doesn't or physically able to.

first have @ specific memory allocator , see if configured homecoming memory system. depends on os , compiler, neither of info provide, question should reply you:

will malloc implementations homecoming free-ed memory system?

whether able depends on fragmentation of heap. total memory blocks can returned scheme tiny allocations dispersed throughout heap prevent (though allocators typically seek avoid this).

qt memory memory-leaks network-programming qsslsocket

No comments:

Post a Comment