PDA

View Full Version : Signals / Slots increase the memory usage with Qt::QueuedConnection



h0nki
1st June 2009, 01:01
Hello,
first of all i want to say hello to this great forum :)

Here my problem (I tried to find it on the internet, but I did not have luck):

If i connect a simple slot (only writes a string on the console) to a signals and emit this signal a lot of times, the program's memory usage increases.
But only, if i connect the signal to the slot with Qt::QueuedConnection. If it is direct connected, the memory sticks at the same size, as if i would never emit the signal.
Why does this happens?
Thank you and good night ;)

Here my code:

main.cpp

#include <QtCore/QCoreApplication>
#include "a.h"

int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
A a;
QObject::connect(&a, SIGNAL(testSig()), &a, SLOT(testSlot())/*, Qt::QueuedConnection*/);
for (int i = 0; i < 50000; ++i) a.test();
return app.exec();
}

a.h

#ifndef A_H
#define A_H

#include <QObject>

class A : public QObject {

Q_OBJECT

public:
A();
void test();

public slots:
void testSlot();

signals:
void testSig();
};

#endif // A_H


a.cpp

#include "a.h"

A::A() {
}

void A::testSlot() {
qDebug("hi");
}

void A::test() {
emit testSig();
}

wysota
1st June 2009, 09:21
Queued signals are implemented using events thus if you emit a signal, an event gets allocated which increases memory usage. But don't worry, after the event is processed, the memory is freed (although you might not notice it in process memory stats).

h0nki
1st June 2009, 10:09
Ok, but why is it not displayed, that the memory is freed? Just wondering ...

lit-uriy
1st June 2009, 10:24
h0nki, For queue the place in memory is required.

lit-uriy
1st June 2009, 10:27
>>why is it not displayed
Where it is not displayed? What tool do you measure the memory consumption?

wysota
1st June 2009, 10:36
Ok, but why is it not displayed, that the memory is freed? Just wondering ...

Because your system's memory manager keeps it assigned to the process in case it wants some more memory. Instead of allocating a new page over and over again it just gives it the one in "cache". In case some other process requests a big chunk of memory this "cached" page will be released and will be allocated to the other process. The same should happen if your application doesn't request any new memory over some period of time.

h0nki
1st June 2009, 13:36
I tried to verify this with 2 programs: one the program, with the xxx events and one that simply allocates mass memory. The os only takes memory from all other programs, but not from the program with the mass events.
Where is my error in reasoning?

wysota
1st June 2009, 18:25
I don't know where is an error in your reasoning :-) The allocation algorithm is more complex than what I said, so trying to force it to do something in so trivial way might be not so easy. I can assure you you are not leaking memory because of the sole fact of using queued connections (provided the events are processed as usual).

h0nki
1st June 2009, 19:59
Thanks wysota, for your helping :)
At least I can sleep now in peace ;)