Solved Qt's QTextEdit has big memory leak problem
-
@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:
- Press button to call
setdata()
once. - Press button to call
cleardata()
once. Preferably have that actually delete and recreatetextEdit
so that we have a clear slate. - Look at process's memory consumption.
- Now repeat step #1 a second time.
- Now repeat step #2 a second time.
- 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?
- Press button to call
-
@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!
-
@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 whattop
orfree
or/proc/meminfo
were showing, I had no interest in whatvalgrind
might or might not indicate. It does depend on what you are trying to investigate. -
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: 180MBsetdata : 279 MB
setdata : 459 MB
cleardata: 262MB ( does not release all )If it can release cleanly , I would appreciate it .
-
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.
-
@cyberpunker And how do you measure?
-
Xfce's task manager
-
quoting @Christian-Ehrlicher:
With a proper tool like e.g. valgrind or heaptrack, not with top nor Task Manager!
Regards
-
any problem with xfce's task manager ?
I run valgrind in qtcreator, it can't endup ,so I give up.
-
@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.
-
@cyberpunker said in Qt's QTextEdit has big memory leak problem:
setdata : 279 MB
cleardata: 180MB ( does not release all )
setdata : 279 MB
cleardata: 180MBThis part at least confirms my suggestion that your program will be able to re-use memory which gets freed, does it not?
-
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.
-
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.