0

Various sources (e.g. this post) make it sound like garbage collection occurs properly for figures created with matplotlib.figure.Figure() but not for figures created with matplotlib.pyplot.figure(). Unfortunately, I haven't been able to find documentation of this.

Is it true that Python will garbage collect old figures created with matplotlib.figure.Figure() that are no longer used? For example...

  • If I assign fig = matplotlib.figure.Figure() inside a function, will the memory for this figure be cleared up when the function ends (assuming I don't do something like return fig)?
  • If I assign fig = matplotlib.figure.Figure(), will the memory for the figure immediately be cleared up if I then assign fig = 23?

Thank you!

(I apologize if this isn't actually "garbage collection." If so, let me know. )

Edit: fixed typo

1 Answers1

2

The matplotlib.pyplot.figure documentation states you should explicitly call pyplot.close on the figures you are not using. Otherwise pyplot will not properly clean up memory. Check out the notes at the bottom here.

0x263A
  • 1,807
  • 9
  • 22
  • Thank you for the answer! But this does not actually address the question. (This may be because the question initially had a typo, now fixed—sorry about that.) – Duncan MacIntyre Jul 20 '21 at 05:25