-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Memory leak in plt.close() when unshown figures in GUI backends are closed #20300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This indeed slowly increases memory usage (checked with memory_profiler and 500 samples instead of 5000): However, the code is not reasonable in itself. It seems you are trying wildly delete everything. But though that, While that doesn't explain the memory leak, let's try and start from a realistic example: What are you trying to do exactly? Do you only want to replace data inside the Axes? If so, the only cleanup command necessary should be |
I am aware that the example code contains redundant clear commands, I wanted to emphasize that I tried all the commands that solved past issues of a similar kind. However, after your remarks regarding a floating axis object I noticed that this might be partially the reason for the issue, and only the following combination of clear commands in each iteration is already enough to result in the memory growth problem: plt.cla()
plt.close() In my use case I only need to clear the axis data, therefore for me the issue would be solved by only using |
Regular usage with clearing the Axes or figure does not have a memory leak. It seems that Axes clearing
Figure clearing
Figure closingSimple figure closing
Closing with garbage collectionStrips of some memory, but doesn't help with the leak. All this does is that the gc slows down execution:
|
I'm relatively sure tK leaking has been documented before. Does this happen for other backends? |
As proposed in #5795, this has to do with created but not-shown GUI windows (all tests done with Qt on linux). It works if you show the windows and close them afterwards
The edges are probably due to garbage collection. It works if you use a non-GUI backend
SummaryThis only happens if you work in a GUI backend, create new figures, but don't show them. This way of usage is not reasonable. There are working usage patterns for all relevant scenarios. I'm closing this as "won't fix" because the memory leak doesn't happen for proper usage, and I assume that debugging the GUI framework memory management would be quite hard. |
gc + circular references is the source of the jagged edges, Python only does the more expensive sweeps every so often and we have a lot of circular references! |
…ke rate. Note to self: Also should check how spikerates are estimated as it seems they have a little bit of a delay of going up after a couple of quick spikes. Also now found a reference to teh memory leak problem which occurs in gui backends but not non-gui such as agg. Allegedly, also does not occur in gui backends if figures are shown and then closed. This does not seem to be tha case for me (see matplotlib/matplotlib#20300 for reference)
- Add parallelism with starmap: script will create plots in parallel with the provided number of tasks via the -p argument - Change example file to be much smaller - Fix problem with memory leakage when making a large number of plots Ref: matplotlib/matplotlib#20300
- Add parallelism with starmap: script will create plots in parallel with the provided number of tasks via the -p argument - Change example file to be much smaller - Fix problem with memory leakage when making a large number of plots Ref: matplotlib/matplotlib#20300
- Add parallelism with starmap: script will create plots in parallel with the provided number of tasks via the -p argument - Change example file to be much smaller - Fix problem with memory leakage when making a large number of plots Ref: matplotlib/matplotlib#20300 - Docstrings for functions
- Add parallelism with starmap: script will create plots in parallel with the provided number of tasks via the -p argument - Change example file to be much smaller - Fix problem with memory leakage when making a large number of plots Ref: matplotlib/matplotlib#20300 - Docstrings for functions
Bug report
Bug summary
When creating multiple plots (e.g. in a loop), the allocated memory for the process increases in each iteration even though the figure is cleared each time.
Code for reproduction
Expected outcome
The allocated memory should not increase in each iteration.
Matplotlib version
import matplotlib; print(matplotlib.__version__)
): 3.4.2print(matplotlib.get_backend())
): TkAggMatplotlib was installed via pip.
The text was updated successfully, but these errors were encountered: