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.
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.
We can clearly see that there are no references to the figure or canvas left after leaving the `DisplayGraph` function, so you'd think all matplotlib components would be gone before tkinter has a chance to render them. Yet, the interactive updates of the resize callback are still working, so they have not been garbage collected. We can see that instances of `FigureCanvasTkAgg` are surviving the garbage collection and growing by one instance each cycle, which just about accounts for the size of the memory leak.
It's no secret that there is a reference cycle here, namely fig.canvas <-> canvas.figure, but the confusing thing is how they are surviving gc.collect. Objgraph (isolating the first-ever FigureCanvasTkAgg) points to two culprits. First, the canvas is referenced in the MouseEvent sitting in LocationEvent.lastevent, which is a long-lived, class-attribute reference in matplotlib.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 FigureCanvasTkAgg alive:
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:
# Can't get destroy events by binding to _tkcanvas. Therefore, bind
# to the window and filter.
deffilter_destroy(event):
ifevent.widgetisself._tkcanvas:
CloseEvent("close_event", self)._process()
root.bind("<Destroy>", filter_destroy, "+")
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 unbind into the logic, at least not without having users make an extra method call they never had to make before.
Activity
richardsheridan commented on 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 FigureManagerTk at all.
If we write the example in a more conventional tkinter style:
Longish codeIt's no secret that there is a reference cycle here, namely fig.canvas <-> canvas.figure, but the confusing thing is how they are surviving gc.collect. Objgraph (isolating the first-ever FigureCanvasTkAgg) points to two culprits. First, the canvas is referenced in the MouseEvent sitting in LocationEvent.lastevent, which is a long-lived, class-attribute reference in matplotlib.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 FigureCanvasTkAgg alive:
Less big object graph imageFrom 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 unbind into the logic, at least not without having users make an extra method call they never had to make before.
CC @tacaswell @anntzer @oscargus
tacaswell commented on Jan 27, 2023
Can we use a weakref in the callbacks rather than self?
richardsheridan commented on Jan 27, 2023
This works but it's a bit ugly? weakref.proxy gave 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 commented on Jan 27, 2023
un-bind as part of the short-circuit so we will run those at most once?