Michiel de Hoon wrote:
Hi everybody,
I was looking at this bug about a memory leak:
https://sourceforge.net/tracker/?func=detail&atid=560720&aid=2889570&group_id=80706
While this bug is now essentially fixed, I noticed that the Figure class contains some circular references that prevent it from being cleaned up by the garbage collector, which is effectively a memory leak. At least, for this code:
from matplotlib.figure import Figure
fig = Figure()
del fig
the garbage collector reports 76 unreachable objects. This seems to be independent of which backend is being used.
Is it worthwhile to look deeper into it? Or should we accept that it is unavoidable that a library of the size of matplotlib will have circular references?
The purpose of the garbage collector is to handle the case of circular references--and mpl is full of circular references. When there are no circular references, then reference counting suffices. The garbage collector fails to handle circular references when an object has a __del__ method. I don't think we have that problem.
I am not actually seeing the result you describe, though. Using python from the interactive prompt--no ipython--when I do gc.collect() and then gc.get_count() I get back to the count from before instantiating the Figure.
>>> gc.get_count()
(4, 0, 0)
>>> gc.get_count()
(5, 0, 0)
>>> gc.get_count()
(5, 0, 0)
>>> gc.get_count()
(5, 0, 0)
>>> fig = Figure()
>>> gc.get_count()
(128, 0, 0)
>>> del(fig)
>>> gc.get_count()
(128, 0, 0)
>>> gc.collect()
76
>>> gc.get_count()
(4, 0, 0)
>>> gc.get_count()
(5, 0, 0)
Eric
···
--Michiel.