Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FigureCanvasTkAgg memory leak #24820

Closed
Milestone
@cenfra-git

Description

@cenfra-git

Bug summary

I'm trying to update a plot in tkinter by destroying the frame where the FigureCanvasTkAgg tk widget is packed and creating another instance of FigureCanvasTkAgg, but the memory of the script only goes up after repeating the process several times.

Code for reproduction

importtkinterastkfromtkinterimportttkfrommatplotlib.backends.backend_tkaggimportFigureCanvasTkAggfrommatplotlib.figureimportFigureimportnumpyasnpclassMain_Program(tk.Tk):def__init__(self,*args,**kwargs):tk.Tk.__init__(self,*args,**kwargs)self.geometry("500x500")# ---------------------------------------- TOP FRAME (Button)self.top_frame=tk.Frame(self)ttk.Button(self.top_frame,text="Update graph",command=self.update_graph).pack()self.top_frame.pack()# ---------------------------------------- BOTTOM FRAME (Graph)self.bottom_frame=tk.Frame(self)DisplayGraph(self.bottom_frame,x_limit= (-10,10) ,delta_x=0.1 ,y_input="2*x+2" )self.bottom_frame.pack()defupdate_graph(self):# ----------------------------- CLEARforwidgetinself.bottom_frame.winfo_children():widget.destroy()#  ----------------------------- DISPLAY NEW GRAPHDisplayGraph(self.bottom_frame,x_limit= (-10,10) ,delta_x=0.1 ,y_input="2*x+2" )classDisplayGraph:def__init__(self,passed_frame,x_limit=(),# TUPLE (-10,10)delta_x=0.0,# FLOAT 0.1y_input='',# STRING 2*x+2figsize_x=6,figsize_y=5):self.x_limit=x_limitself.delta_x=delta_xself.y_input=y_input# ---------------------------------------------------------- CREATE FIGURE AND SUBPLOTself.fig=Figure(figsize=(figsize_x,figsize_y),dpi=100)self.ax=self.fig.add_subplot(111)self.ax.grid()# ---------------------------------------------------------- PLOTself.plot_input()# ---------------------------------------------------------- PACK AND DRAWself.canvas_frame=tk.Frame(passed_frame,bg="black")self.canvas_mat=FigureCanvasTkAgg(self.fig,master=self.canvas_frame)self.canvas_mat.get_tk_widget().pack(anchor="center",expand=True,padx=2,pady=2)self.canvas_frame.pack(side=tk.LEFT,anchor="center",expand=True)self.canvas_mat.draw()# ----------------------------------------------------------defplot_input(self):x=np.arange(self.x_limit[0],self.x_limit[1],self.delta_x)y=eval(self.y_input)self.ax.plot(x,y)App=Main_Program()App.mainloop()

Actual outcome

Memory after running the script for the first time
image

Memory after clicking the button 20 times:
image

I've seen that some of the memory is freed after a while but not immediately after updating the frame.

Expected outcome

I would expect that destroying the frame where the FigureCanvasTkAgg widget is located would allow me to create another instance of FigureCanvasTkAgg without increasing the memory (or at least too much).

Additional information

I've seen posts as far back as 13 years ago reporting the issue in different places, but I have not been able to find a way to solve it. I know that you can create only one instance of the FigureCanvasTkAgg object and clear/plot each time, but I'm working on a project that involves several figures at the same time and closing the tkinter toplevel window does not seem to clear the memory.

Operating system

Windows

Matplotlib Version

3.6.2

Matplotlib Backend

TkAgg

Python version

3.11.0

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp