today I realized that although you delete the content from a Text widget, the RAM memory used by it, is never being released and it's a really issue from my point of view.
to make the issue clear, I replicated it in a simple software. below you can see the source code:
from tkinter import *
from tkinter import ttk, scrolledtext
import ipaddress
class Test:
def __init__(self):
self.root = Tk()
self.root.geometry("400x300")
self.style=ttk.Style()
self.style.configure("TButton", padding=0, highlightthickness=0, borderwidth=0)
self.MyButton = ttk.Button(self.root, text="Start the program", width=20, command=self.heavy_process)
self.MyButton.place(x=16, y=16)
self.MyLabel = ttk.Label(self.root, text="Note: I'm ready to start!")
self.MyLabel.place(x=16, y=64)
self.MyText=scrolledtext.ScrolledText(self.root, width=20, height=5)
self.MyText.place(x=16, y=128)
self.ResetMemoryButton = ttk.Button(self.root, text="Reset Memory", width=20, command=lambda:self.MyText.forget())
self.ResetMemoryButton.place(x=228, y=100)
self.root.mainloop()
def heavy_process(self):
self.MyButton.configure(state="disable")
self.MyLabel.configure(text="I'm working, please wait..")
self.root.update() # whithout this instruction the last two ones, will be not implemented.. why?
obj=ipaddress.ip_network("10.0.0.0/8")
ls=[]
for obj in list(obj.hosts()):
ls.append(obj.exploded)
self.MyText.insert("1.0", "\n".join(ls))
self.MyButton.configure(state="enable")
self.MyLabel.configure(text="Note: Task completed! Now I'm ready to start again!")
if __name__=="__main__":
app=Test()
when you click on Start the program
, the software will fill the text widget with a large amount of information, in particular all IPv4 addresses contained in the 10.0.0.0/8 network. to do it, the software needs around one minute.
when this step is completed, the RAM memory used by the text widget will be really big and the situation will not change even if you delete the text widget content manually:
if I save the text widget content in a text file, its size is 235 MB, so why Tkinter need around 3 GB to manage this kind of information?
on Internet I read about the .forget()
and .edit_reset()
methods to delete the useless memory, but they don't work (see my lambda function). I really don't know how to solve this issue.. any ideas?