Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt's QTextEdit has big memory leak problem

Qt's QTextEdit has big memory leak problem

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 2.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cyberpunkerC Offline
    cyberpunkerC Offline
    cyberpunker
    wrote on last edited by
    #1

    In my object, I display the log file in a QTextEdit widget. Then I find the program consumption hundres MB of memory even the log file size is small . And the memory is not released when its content is cleared.

    Then I write a small demo in the QtCreator with Qt5.12.x , just put a QTextEdit widget on it. call the append function to inject QString in it , the problem recurr, even I delete the QTextEdit widget.

    MainWindow::MainWindow(QWidget *parent)
    {

    QPushButton *btn1  = new QPushButton(this) ;
    QPushButton *btn2  = new QPushButton(this) ;
    
    txtEdit = new QTextEdit(this) ;
    
    
    QHBoxLayout *layout = new QHBoxLayout ;
    layout->addWidget( btn1 ) ;
    layout->addWidget( btn2 ) ;
    layout->addWidget(txtEdit ) ;
    
    QWidget *widget = new QWidget;
    widget->setLayout(layout);
    
    setCentralWidget(widget);
    
    connect( btn1 , SIGNAL( clicked(  ) ) , this , SLOT( setdata( ) ) ) ;
    connect( btn2 , SIGNAL( clicked(  ) ) , this , SLOT( cleardata( ) ) ) ;
    

    }

    void MainWindow::setdata( )
    {

    for ( int ii = 0 ; ii < 50 ; ii++ )
    {
    
    
        for ( int i = 0 ; i < 500 ; i++ )
        {
    
           QString str ;
    
           for ( int j = 0 ; j < 100 ; j++ )
           {
    
               str = str+ QString::number( j ) ;
           }
    
            txtEdit->append( str ) ;
    
        }
    
    }
    

    }

    void MainWindow::cleardata( )
    {

    txtEdit->clear() ;
    
    //txtEdit->close( ) ;
    
    //delete  txtEdit ;
    

    }

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      There is no leak, QTextEdit allows undo/redo operations so the previous stuff must be saved somewhere - see also https://bugreports.qt.io/browse/QTBUG-72123 and https://doc.qt.io/qt-5/qtextedit.html#undoRedoEnabled-prop

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      6
      • cyberpunkerC Offline
        cyberpunkerC Offline
        cyberpunker
        wrote on last edited by
        #3

        Sorry , I don't understand what 'undoRedoEnabled ' means ,can I resolve this memory problem by setting it ?

        can you explain it more ? Thanks

        JonBJ 1 Reply Last reply
        0
        • cyberpunkerC cyberpunker

          Sorry , I don't understand what 'undoRedoEnabled ' means ,can I resolve this memory problem by setting it ?

          can you explain it more ? Thanks

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @cyberpunker
          Yes, put in txtEdit->setUndoRedoEnabled(false) when you create it and see if it improves.

          QTextEdit by default allows undo (Ctrl+Z)/redo. It has to save the old or new contents every time there is a change to permit that, and that undo history takes up memory. Your code does thousands (2.5 million actually) of modifications to the text, so loads to save for undoing.

          Disabling the undo/redo feature will mean it does not use that memory.

          1 Reply Last reply
          4
          • W Offline
            W Offline
            wrosecrans
            wrote on last edited by
            #5

            @cyberpunker said in Qt's QTextEdit has big memory leak problem:

            Then I find the program consumption hundres MB of memory

            How are you measuring this? It may just be that you are seeing erroneous numbers. Qt has some large shared libraries that are mapped into your rpocess address space, but not "really" consuming any physical memory.

            cyberpunkerC 1 Reply Last reply
            0
            • W wrosecrans

              @cyberpunker said in Qt's QTextEdit has big memory leak problem:

              Then I find the program consumption hundres MB of memory

              How are you measuring this? It may just be that you are seeing erroneous numbers. Qt has some large shared libraries that are mapped into your rpocess address space, but not "really" consuming any physical memory.

              cyberpunkerC Offline
              cyberpunkerC Offline
              cyberpunker
              wrote on last edited by
              #6

              @wrosecrans
              I call setUndoRedoEnabled(false) just after construction , then it still can not return all the memory when called clear() . So ,there is no improvment.
              I do this on a debian, and I see its status in the task manager.

              JonBJ 1 Reply Last reply
              0
              • cyberpunkerC cyberpunker

                @wrosecrans
                I call setUndoRedoEnabled(false) just after construction , then it still can not return all the memory when called clear() . So ,there is no improvment.
                I do this on a debian, and I see its status in the task manager.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @cyberpunker
                Well, it makes sense to me that disabling the undo buffer should save a lot of memory in the case of what your code does. But you say not.

                You cannot assume that memory consumed by a C++/Qt program will be returned to the operating system on freeing variables. It would not be unexpected that this memory is returned to the pool of available memory to be reused by your program, but not deallocated from your program back to the OS. That's not how memory deallocation typically works.

                What you should test is:

                1. Press button to call setdata() once.
                2. Press button to call cleardata() once. Preferably have that actually delete and recreate textEdit so that we have a clear slate.
                3. Look at process's memory consumption.
                4. Now repeat step #1 a second time.
                5. Now repeat step #2 a second time.
                6. Now look at memory consumption again. Is it twice the amount which was used when measured at step #3? Hopefully (but not guaranteed) it will not be twice the amount, because the memory allocated during step #4 will have a chance to re-use the memory allocated in step #1 which was freed in step #2?
                cyberpunkerC 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #8

                  @JonB said in Qt's QTextEdit has big memory leak problem:

                  Look at process's memory consumption.

                  With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  JonBJ cyberpunkerC 2 Replies Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    @JonB said in Qt's QTextEdit has big memory leak problem:

                    Look at process's memory consumption.

                    With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    Well, to be fair, yes and no. I am perfectly aware of the distinction. However, as was the case recently in another investigation of mine, what I was interested in was indeed the consumption of free memory from the OS rocketing in a Qt app. I precisely did want to know what top or free or /proc/meminfo were showing, I had no interest in what valgrind might or might not indicate. It does depend on what you are trying to investigate.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @cyberpunker
                      Well, it makes sense to me that disabling the undo buffer should save a lot of memory in the case of what your code does. But you say not.

                      You cannot assume that memory consumed by a C++/Qt program will be returned to the operating system on freeing variables. It would not be unexpected that this memory is returned to the pool of available memory to be reused by your program, but not deallocated from your program back to the OS. That's not how memory deallocation typically works.

                      What you should test is:

                      1. Press button to call setdata() once.
                      2. Press button to call cleardata() once. Preferably have that actually delete and recreate textEdit so that we have a clear slate.
                      3. Look at process's memory consumption.
                      4. Now repeat step #1 a second time.
                      5. Now repeat step #2 a second time.
                      6. Now look at memory consumption again. Is it twice the amount which was used when measured at step #3? Hopefully (but not guaranteed) it will not be twice the amount, because the memory allocated during step #4 will have a chance to re-use the memory allocated in step #1 which was freed in step #2?
                      cyberpunkerC Offline
                      cyberpunkerC Offline
                      cyberpunker
                      wrote on last edited by cyberpunker
                      #10

                      @JonB

                      I change the QTextEdit to QPlainTextEdit .

                      My test :
                      bootup : 56MB ( it is too big I think , just so little a demo )

                      setdata : 279 MB
                      cleardata: 180MB ( does not release all )

                      setdata : 279 MB
                      cleardata: 180MB

                      setdata : 279 MB
                      setdata : 459 MB
                      cleardata: 262MB ( does not release all )

                      If it can release cleanly , I would appreciate it .

                      JonBJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @JonB said in Qt's QTextEdit has big memory leak problem:

                        Look at process's memory consumption.

                        With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!

                        cyberpunkerC Offline
                        cyberpunkerC Offline
                        cyberpunker
                        wrote on last edited by cyberpunker
                        #11

                        @Christian-Ehrlicher

                        I compile and run the Application.exe of Qt's example which is a simple text edit, repeat paste text into it , make the memory consumption roket up , then delete them all , the phenomenon recurrs.

                        aha_1980A 1 Reply Last reply
                        0
                        • cyberpunkerC cyberpunker

                          @Christian-Ehrlicher

                          I compile and run the Application.exe of Qt's example which is a simple text edit, repeat paste text into it , make the memory consumption roket up , then delete them all , the phenomenon recurrs.

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @cyberpunker And how do you measure?

                          Qt has to stay free or it will die.

                          cyberpunkerC 1 Reply Last reply
                          0
                          • aha_1980A aha_1980

                            @cyberpunker And how do you measure?

                            cyberpunkerC Offline
                            cyberpunkerC Offline
                            cyberpunker
                            wrote on last edited by
                            #13

                            @aha_1980

                            Xfce's task manager

                            aha_1980A 1 Reply Last reply
                            0
                            • cyberpunkerC cyberpunker

                              @aha_1980

                              Xfce's task manager

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @cyberpunker

                              quoting @Christian-Ehrlicher:

                              With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!

                              Regards

                              Qt has to stay free or it will die.

                              cyberpunkerC 1 Reply Last reply
                              0
                              • aha_1980A aha_1980

                                @cyberpunker

                                quoting @Christian-Ehrlicher:

                                With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!

                                Regards

                                cyberpunkerC Offline
                                cyberpunkerC Offline
                                cyberpunker
                                wrote on last edited by
                                #15

                                @aha_1980

                                any problem with xfce's task manager ?

                                I run valgrind in qtcreator, it can't endup ,so I give up.

                                aha_1980A 1 Reply Last reply
                                0
                                • cyberpunkerC cyberpunker

                                  @aha_1980

                                  any problem with xfce's task manager ?

                                  I run valgrind in qtcreator, it can't endup ,so I give up.

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @cyberpunker said in Qt's QTextEdit has big memory leak problem:

                                  any problem with xfce's task manager ?

                                  The OS does not display all freed memory instantly. Only if new memory requirements appear, the freed memory is taken into account.

                                  So what you see in task manager is only updated if required.

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  4
                                  • cyberpunkerC cyberpunker

                                    @JonB

                                    I change the QTextEdit to QPlainTextEdit .

                                    My test :
                                    bootup : 56MB ( it is too big I think , just so little a demo )

                                    setdata : 279 MB
                                    cleardata: 180MB ( does not release all )

                                    setdata : 279 MB
                                    cleardata: 180MB

                                    setdata : 279 MB
                                    setdata : 459 MB
                                    cleardata: 262MB ( does not release all )

                                    If it can release cleanly , I would appreciate it .

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @cyberpunker said in Qt's QTextEdit has big memory leak problem:

                                    setdata : 279 MB
                                    cleardata: 180MB ( does not release all )
                                    setdata : 279 MB
                                    cleardata: 180MB

                                    This part at least confirms my suggestion that your program will be able to re-use memory which gets freed, does it not?

                                    1 Reply Last reply
                                    1
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      Hi,

                                      Since it's a log and you likely are going to scroll through it, you might want to consider using a QListView and have one row per line of your log.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      2
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #19

                                        Hi
                                        When using TextEdit as log viewer , its often a good idea to
                                        use
                                        setUndoRedoEnabled(false);
                                        so it doesn't keep a copy around to be able to undo text changes.

                                        Oh. sorry. already mentioned.

                                        1 Reply Last reply
                                        1

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved