Clear used RAM for Tkinter TEXT widget

python tkinter clear screen
tkinter clear label
tkinter delete frame contents
tkinter clear entry on click
how to clear all widgets in tkinter
delete a tkinter widget
reset button in tkinter
tkinter clear text widget

In my GUI I create a ttk.notebook and then put Text widgets into each tab and dump logs/info into these Text widgets. If the process is restarted with a new set of log files I .forget() the old tabs and then create the new ones. I've noticed that the ram used from the original Text widgets is not cleared when I do this. How can I free this ram?

Example:

from tkinter import *
from tkinter.ttk import Notebook
main = Tk()
def maker():
    nb = Notebook(main, name='nbook')
    nb.grid(row=0, column=0, columnspan=2)
    frame1 = Frame(name='frame1')
    txt = Text(frame1, wrap=NONE)
    txt.grid()
    nb.add(frame1, text='textwindow')
    txt.insert('end', 'hello\n' * 1000000)

def remover():
    print(main.winfo_children())
    for tab in main.children['nbook'].tabs():
        main.children['nbook'].forget(tab)
    print(main.winfo_children())

b1 = Button(main, text='Insert', command=maker)
b1.grid(row=1, column=0)
b2 = Button(main, text='Clear tabs', command=remover)
b2.grid(row=1, column=1)
main.mainloop()

In the example, when you click "Insert" it creates the widget and dumps a bunch of text into it. If you call remover with the "Clear tabs" button it forgets all tabs but the ram used by that text is never free. Even if you destroy the text widget the ram is not released. How can I free this up?

Text.delete('1.0', END)
Text.edit_reset()

Will clear the history of the Text widget, so call this after delete. Hope this solves your problem

python - Clear used RAM for Tkinter TEXT widget, In my GUI I create a ttk.notebook and then put Text widgets into each tab and dump logs/info into these Text widgets. If the process is restarted  Created: November-22, 2019 . Hide and Recover Tkinter Widgets Delete Tkinter Widgets Permanently by Clicking a Button In this tutorial, we will demonstrate how to hide, recover the Tkinter widgets, by clicking a button.

Destroying entire (set of) widget(s) when you only need to configure one of their attributes sounds like a very poor design, and such practice is notorious for resulting in pricey memory leaks, as you have already seen.

In any case, I reckon you would be much better off by simply configuring the widgets you already have, as opposed to destroying/recreating them when the changes are due.

From what I've been able to comprehend, you said you need to frequently display some text (namely, logs?) in the Text widget, so why not just clear the text you already have and insert the new one into the widget:

txt.delete('1.0', 'end') # clearing the text widget
txt.insert('end', my_log) # inserting the log into the text widget

How to Delete Tkinter Text Box's Contents, delete method of Tkinter Text Box widget deletes the characters of the text box, and its first and last argument specifies the range of the text to  Additionally, if you delete all the text from a Text widget it does not free the used ram. So if one set of logs used 2 GB of ram and the next uses 500MB of ram the application is still holding onto 2GB of ram from before. That is the memory that im trying to free up. – sidnical Dec 27 '17 at 15:23

I later found that just doing a .forget() on the tabs does not get rid of the memory used by the widgets. This may be because they are referenced somewhere else but I couldn't find the source. If I .destroy() the widgets from the tab before doing the .forget() on the tab itself then the memory is eventually freed by garbage collector. For my situation, running a new set of logs through the tool reuses the currently used memory and the extra(if any) is freed after it sits for a bit.

python, Label(self.frame, text="Label") test_label.grid(row=1, column=0) was a class inherited from the tkinter Frame class being used as a Toplevel,  So let's add a scrollbar to our window. To this purpose, Tkinter provides the Scrollbar () method. We call it with the root object as the only parameter. To die, to sleep-- No more--and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to.

memory leaks in Tkinter, I noticied some memory leak in text widget of Tkinter : Python 2.1.1 (#1, I think the term "memory leak" is usually used when something can make the process usage, you'll notice that no storage is freed after '.t delete 1.0 end'. I suspect that  Embedded object name can be used to refer to windows and images embedded in the text widget. To refer to a window, simply use the corresponding Tkinter widget instance as an index. To refer to an embedded image, use the corresponding Tkinter PhotoImage or BitmapImage object.

Python Tkinter - Entry Widget, The Entry Widget is a Tkinter Widget used to Enter or dispay a single line of text. the entry's current text as a string. delete() : Deletes characters from the widget​  Python tkinter widgets [ 12 exercises with solution] [An editor is available at the bottom of the page to write and execute the scripts.1. Write a Python GUI program to add a button in your application using tkinter module.

Python, Out of all the GUI methods, tkinter is most commonly used method. in the text entry box Try and except statement is used Function to clear the contents. Toplevel widgets in tkinter are just like Frame, but they are always contained in a new window. Those windows have title names, border and all the other features of a regular window. Those windows have title names, border and all the other features of a regular window.

Comments
  • Long story short - it's a known "bug" and you can do nothing about it.
  • Thank you @CommonSense
  • By doing this and then destroying im actually seeing a 2MB increase in ram used instead of any drop.
  • Is doing a .forget() on a tab the same as destroying? Each time a set of logs is parsed a different amount of tabs are created. If the second time only needs 2 tabs but 9 are shown I would still have 7 tabs that need to be removed because they arent being used. Additionally, if you delete all the text from a Text widget it does not free the used ram. So if one set of logs used 2 GB of ram and the next uses 500MB of ram the application is still holding onto 2GB of ram from before. That is the memory that im trying to free up.
  • Does this give you memory leaks? tkinter.tk.call(widget, 'delete', index1, index2)?
  • if I use 1.0 and END for the indexes this deletes all the text in the Text widget but the used memory does not change. Same as doing txt.delete('1.0', 'end'). I think thats what you were after.