Results 1 to 9 of 9

Thread: Signals / Slots increase the memory usage with Qt::QueuedConnection

  1. #1
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Signals / Slots increase the memory usage with Qt::QueuedConnection

    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
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "a.h"
    3.  
    4. int main(int argc, char *argv[]) {
    5. QCoreApplication app(argc, argv);
    6. A a;
    7. QObject::connect(&a, SIGNAL(testSig()), &a, SLOT(testSlot())/*, Qt::QueuedConnection*/);
    8. for (int i = 0; i < 50000; ++i) a.test();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    a.h
    Qt Code:
    1. #ifndef A_H
    2. #define A_H
    3.  
    4. #include <QObject>
    5.  
    6. class A : public QObject {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11. A();
    12. void test();
    13.  
    14. public slots:
    15. void testSlot();
    16.  
    17. signals:
    18. void testSig();
    19. };
    20.  
    21. #endif // A_H
    To copy to clipboard, switch view to plain text mode 

    a.cpp
    Qt Code:
    1. #include "a.h"
    2.  
    3. A::A() {
    4. }
    5.  
    6. void A::testSlot() {
    7. qDebug("hi");
    8. }
    9.  
    10. void A::test() {
    11. emit testSig();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by h0nki; 1st June 2009 at 01:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    Ok, but why is it not displayed, that the memory is freed? Just wondering ...

  4. #4
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    h0nki, For queue the place in memory is required.

  5. #5
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    >>why is it not displayed
    Where it is not displayed? What tool do you measure the memory consumption?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    Quote Originally Posted by h0nki View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    h0nki (1st June 2009)

  10. #9
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals / Slots increase the memory usage with Qt::QueuedConnection

    Thanks wysota, for your helping
    At least I can sleep now in peace

Similar Threads

  1. Bad memory usage on QWebView I think
    By jiturra in forum Qt Programming
    Replies: 15
    Last Post: 21st January 2014, 20:35
  2. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  3. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  4. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  5. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.