FigureCanvasTkAgg的声明导致内存泄漏 [英] Declaration of FigureCanvasTkAgg causes memory leak
问题描述
我很难弄清楚为什么FigureCanvasTkAgg的声明会导致内存泄漏,我的类__init__
方法中有以下几行:
I'm having difficulty figuring out just why the declaration of FigureCanvasTkAgg causes a memory leak, I have the following lines in my class __init__
method:
# pndwinBottom is a paned window of the main screen
self.__drawplotFrame = Frame(pndwinBottom, width=WIDTH, height=HEIGHT) # the frame on which we will add our canvas for drawing etc.
self.__fig = plt.figure(figsize=(16,11))
self.__drawplotCanvas = FigureCanvasTkAgg(self.__fig, master=self.__drawplotFrame)
问题在于,运行我的应用程序并退出后,python32.exe保留在我的进程窗口中,并且会阻塞我的计算机.但是,注释掉这三行将允许我的应用程序退出,并且该过程将正确终止.这些行可能会对我的应用程序造成什么影响,从而阻止该过程在应用程序完成之后结束?谢谢
the problem is that upon running my application , and exiting, python32.exe remains in my process window and will clog up my computer. Commenting out these three lines however will allow my application to exit and the process will correctly terminate. What could these lines be doing to my application that prevents the process from ending after the application is finished? Thanks
编辑
内存泄漏似乎仅由self.__fig = plt.figure(figsize=(16, 11))
行引起.在退出之前,我是否需要对plt进行某种解构?
The memory leak seems to be caused by only the line self.__fig = plt.figure(figsize=(16, 11))
. Do I need to do some sort of deconstruction with plt before exiting?
推荐答案
我想这是由于Tkinter窗口关闭时pyplot图形没有被破坏引起的.
像在在tk示例中嵌入一样,尝试使用Figure
:
I'm gonna guess this is caused by the pyplot figure not being destroyed when the Tkinter window is closed.
Like in the embedding in tk example try using Figure
:
from matplotlib.figure import Figure
self.__fig = Figure(figsize=(16,11))
示例用法:
import Tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class App():
def __init__(self, parent):
self.__drawplotFrame = tk.Frame(parent, width=500, height=500)
self.__drawplotFrame.pack()
self.__fig = Figure(figsize=(16,11))
self.__p = self.__fig.add_subplot(1,1,1)
self.__p.plot(range(10), range(10))
self.__drawplotCanvas = FigureCanvasTkAgg(self.__fig, master=self.__drawplotFrame)
self.__drawplotCanvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
root = tk.Tk()
App(root)
root.mainloop()
这篇关于FigureCanvasTkAgg的声明导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!