Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何从Tkinter wigets使用的无用信息中清除RAM内存?_Python 3.x_Tkinter_Memory_Text_Widget - Fatal编程技术网

Python 3.x 如何从Tkinter wigets使用的无用信息中清除RAM内存?

Python 3.x 如何从Tkinter wigets使用的无用信息中清除RAM内存?,python-3.x,tkinter,memory,text,widget,Python 3.x,Tkinter,Memory,Text,Widget,今天我意识到,虽然你从一个文本小部件中删除了内容,但它使用的RAM内存永远不会被释放,从我的观点来看,这是一个真正的问题 为了澄清这个问题,我在一个简单的软件中复制了它。您可以在下面看到源代码: from tkinter import * from tkinter import ttk, scrolledtext import ipaddress class Test: def __init__(self): self.root = Tk() self.

今天我意识到,虽然你从一个文本小部件中删除了内容,但它使用的RAM内存永远不会被释放,从我的观点来看,这是一个真正的问题

为了澄清这个问题,我在一个简单的软件中复制了它。您可以在下面看到源代码:

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()
当您单击启动程序时,软件将用大量信息填充文本小部件,特别是10.0.0.0/8网络中包含的所有IPv4地址。要做到这一点,软件大约需要一分钟

完成此步骤后,文本小部件使用的RAM内存将非常大,即使手动删除文本小部件内容,情况也不会改变:

如果我将文本小部件内容保存在一个文本文件中,它的大小是235MB,那么为什么Tkinter需要大约3GB来管理此类信息呢

在互联网上,我读到了有关删除无用内存的
.forget()
.edit_reset()
方法的文章,但这些方法不起作用(请参阅我的lambda函数)。我真的不知道如何解决这个问题。。有什么想法吗