Repository navigation
Description
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
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
class Main_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()
def update_graph(self):
# ----------------------------- CLEAR
for widget in self.bottom_frame.winfo_children():
widget.destroy()
# ----------------------------- DISPLAY NEW GRAPH
DisplayGraph(self.bottom_frame,
x_limit= (-10, 10) ,
delta_x= 0.1 ,
y_input= "2*x+2" )
class DisplayGraph:
def __init__(self,passed_frame,
x_limit=(), # TUPLE (-10,10)
delta_x=0.0, # FLOAT 0.1
y_input='', # STRING 2*x+2
figsize_x=6,
figsize_y=5):
self.x_limit = x_limit
self.delta_x = delta_x
self.y_input = y_input
# ---------------------------------------------------------- CREATE FIGURE AND SUBPLOT
self.fig = Figure(figsize=(figsize_x,figsize_y),dpi=100)
self.ax = self.fig.add_subplot(111)
self.ax.grid()
# ---------------------------------------------------------- PLOT
self.plot_input()
# ---------------------------------------------------------- PACK AND DRAW
self.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()
# ----------------------------------------------------------
def plot_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

Memory after clicking the button 20 times:

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
Activity
richardsheridan commentedon Jan 27, 2023
This version of the tk memory leak is even more fundamental than some of the other open issues, because it doesn't involve
FigureManagerTkat all.If we write the example in a more conventional tkinter style:
Longish code
It's no secret that there is a reference cycle here, namely
fig.canvas <-> canvas.figure, but the confusing thing is how they are survivinggc.collect. Objgraph (isolating the first-everFigureCanvasTkAgg) points to two culprits. First, the canvas is referenced in theMouseEventsitting inLocationEvent.lastevent, which is a long-lived, class-attribute reference inmatplotlib.backend_bases:Big object graph image
(likely better viewed in its own window. Look for the red arrow!)

It would be better if this reference didn't live so long, but we can overwrite it by clicking somewhere in the plot before hitting the update button again. This cleans up the graph and reveals the only other things holding
FigureCanvasTkAggalive:Less big object graph image
From the perspective of the python object graph, these are "leaks" since they have no referents. but what it actually means in this specific case is that the references are "owned" by the tkinter c code.
Fortunately, we can see which methods are referenced by the leaks specifically, directing us to the culprit binds:
matplotlib/lib/matplotlib/backends/_backend_tk.py
Lines 201 to 208 in 84cc898
These are not cleaned up by the
widget.destroy()logic because they are bound to the toplevel window rather than the canvas. If you comment them out, the leaks shrink by 99%.But at this point, I'm stuck, because we sorta need those?! and it's not obvious how we could sneak an
unbindinto the logic, at least not without having users make an extra method call they never had to make before.CC @tacaswell @anntzer @oscargus
tacaswell commentedon Jan 27, 2023
Can we use a weakref in the callbacks rather than
self?richardsheridan commentedon Jan 27, 2023
This works but it's a bit ugly?
weakref.proxygave various errors. is there a better way?Also it would be good to perform an unbind rather than just short-circuiting the existing bind function.
tacaswell commentedon Jan 27, 2023
un-bind as part of the short-circuit so we will run those at most once?