Qt 服务器内存增长

Qt 服务器内存增长,qt,memory,memory-leaks,network-programming,qsslsocket,Qt,Memory,Memory Leaks,Network Programming,Qsslsocket,我正在用C++/Qt开发下载服务器。我面临着记忆增长的问题。在这里,我将分享示例服务器应用程序来演示这个问题 当客户端连接时,它开始每秒发送10Kb的数据块。当客户端断开连接时,套接字将被删除 #include <QCoreApplication> #include <QtNetwork> class Client: public QObject { Q_OBJECT public: Client(QSslSocket *sock) {

我正在用C++/Qt开发下载服务器。我面临着记忆增长的问题。在这里,我将分享示例服务器应用程序来演示这个问题

当客户端连接时,它开始每秒发送10Kb的数据块。当客户端断开连接时,套接字将被删除

#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 data 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";

    return a.exec();
}

#include "main.moc"
#包括
#包括
类客户端:公共QObject
{
Q_对象
公众:
客户端(QSslSocket*sock)
{
此->计时器=新的QTimer(此);
此->定时器->设置间隔(1000);
连接(此->计时器,信号(超时()),此,插槽(sendData());
这个->袜子=袜子;
连接(插座,信号(断开()),此,信号(断开());
连接(sock,SIGNAL(encrypted()),this->timer,SLOT(start());
连接(sock,SIGNAL(readyRead()),this,SLOT(readData());
}
~Client()
{
删除此->袜子;
}
void start()
{
此->sock->启动服务器加密();
}
信号:
无效断开连接();
专用插槽:
void sendData()
{             
qDebug()sock->bytesToWrite())
返回;
QByteArray-ba(10*1024,'1');
此->袜子->写入(ba);
}
void readData()
{
此->sock->readAll();
}
私人:
QSslSocket*袜子;
QTimer*定时器;
}; // 客户
类服务器:公共QTcpServer
{
Q_对象
公众:
服务器()
{     
此->totalConnected=0;
此->实例计数器=0;
}
受保护的:
虚空进线连接(INTD);
私人:
国际联网;
int实例计数器;
专用插槽:
void handleClientDisconnected();
void handleDestroyed();
}; // 服务器
无效服务器::输入连接(int d)
{    
QSslSocket*sock=新QSslSocket(此);
如果(!sock->setSocketDescriptor(d))
{
删除sock;
返回;
}
++这->实例计数器;
qDebug()开始();
}
void服务器::handleClientDisconnected()
{
qDebug()deleteLater();
}
void服务器::handleDestroyed()
{
qDebug()可以将内存分配器配置为将未使用的内存块返回到系统,这是正确的,但实际上它们不会或无法返回

首先,您必须查看您的特定内存分配器,并查看它是否配置为将内存返回到系统。这取决于您的操作系统和编译器,两者都不是您提供的信息,但这个问题应该为您解答:

它是否能够返回取决于堆的碎片。只有完整的内存块才能返回到系统,因此分散在堆中的微小分配将阻止这一点(尽管分配器通常试图避免这一点)