High Memory Usage Issue in Qt Remote Object

98 Views Asked by At

I have recently been learning how to use Qt Remote Objects and have written a small program based on the official documentation: a client-server architecture instant messaging application.

The code is as follows: server Header File:

#ifndef SIMPLESWITCH_H
#define SIMPLESWITCH_H

#include <QByteArray>
#include "rep_simpleswitch_source.h"

class SimpleSwitch : public SimpleSwitchSimpleSource
{
   Q_OBJECT
public:
   SimpleSwitch(QObject* parent = nullptr);
    ~SimpleSwitch();

    void byteArrayTest(char nChar);

public Q_SLOTS:
   void timeout_slot();

private:
   QTimer* stateChangeTimer;
};

#endif // SIMPLESWITCH_H

server Source File:

#include "simpleswitch.h"
#include <QTimer>
#include <QSignalSpy>

SimpleSwitch::SimpleSwitch(QObject* parent) : SimpleSwitchSimpleSource(parent)
{
    stateChangeTimer = new QTimer(this);
    QObject::connect(stateChangeTimer, &QTimer::timeout, this, &SimpleSwitch::timeout_slot);
    stateChangeTimer->start(2000);
}

SimpleSwitch::~SimpleSwitch()
{
    stateChangeTimer->stop();
}

void SimpleSwitch::timeout_slot()
{
    static int nCount = 1;
    if(nCount == 5)
    {
        qDebug() << "nCount = "<< nCount;
        byteArrayTest(1);
    }
    if (nCount == 10)
    {
        qDebug() << "nCount = " << nCount;
        QByteArray byteArray;
        byteArray.append(1);

        setByteArray(byteArray);
        //③ Memory has become 130MB

        stateChangeTimer->stop();
    }
    ++nCount;
}

void SimpleSwitch::byteArrayTest(char nChar)
{
    QByteArray byteArray;


    for(int index = 0; index < 65535 * 1000; ++index)
    {
        byteArray.append(nChar+index);
    }
    //① Allocated 63MB of memory
    // 65535 * 1000 / 1024 / 1024 = 63M

    
    setByteArray(byteArray);
    //② The memory has increased to 302MB


    //after ② Memory has returned to 194MB
}


client:

#ifndef CLIENT_H
#define CLIENT_H

#include <QObject>
#include <QSharedPointer>
#include "rep_simpleswitch_replica.h"

class Client : public QObject
{
    Q_OBJECT
public:
    Client(QSharedPointer<SimpleSwitchReplica> ptr)
            : QObject(nullptr), reptr(ptr)
    {
        QObject::connect(reptr.data(), &SimpleSwitchReplica::byteArrayChanged, this, &Client::onByteArrayChanged);
    }

    ~Client() { }

public Q_SLOTS:
    void onByteArrayChanged(QByteArray byteArray)
    {
        qDebug() << "onByteArrayChanged";
    }

private:
    QSharedPointer<SimpleSwitchReplica> reptr;
};

#endif // CLIENT_H

rep file:

class SimpleSwitch
{
    PROP(QByteArray byteArray);
};

The client responds to the byteArrayChanged signal with the onByteArrayChanged function, which does nothing and is an empty function.

The memory changes are shown in the following figure: enter image description here

Why is the memory changing like this? Why is so much memory needed? How can we bring down the memory usage?

1

There are 1 best solutions below

2
On

I ran this code on Qt 6.2.4 and it worked fine. It seems like a bug in Qt 5.15.I can't migrate the project to Qt 6 (Qt 6 doesn't support Windows 7).I'm wondering if it's possible to compile the Remote Objects module of Qt 6 separately and then use it in Qt 5?Or maybe there's a better idea?