-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Memory leak when plotting multiple figures with the macOS backend #19769
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
|
|
If you throw a
but, if you are running this in a tight loop like the example you are never letting the event loop run so you have N GUI windows hanging out waiting to be cleared. |
Hey, Nope, even with a |
Perhaps try using |
I certainly can do that. Here's the result for the last two iterations of the loops (deltas are the same from one iteration to the next), with
It does look like matplotlib objects to me. |
I did a bit of digging, and the back reference graphs I get tracing from So I suspect that a reference to Here's the code I'm using if anyone's interested: import gc
import random
import matplotlib.pyplot as plt
import objgraph
import matplotlib
matplotlib.use('macosx')
def plot_expr(plot_name):
fig = plt.figure(figsize=(9, 8))
plt.close('all')
objgraph.show_growth(limit=50)
if plot_name == 'leak_2':
objgraph.show_backrefs(
random.choice(objgraph.by_type('Figure')),
filename='chain_mac.png', max_depth=50, refcounts=True)
if __name__ == "__main__":
for i in range(3):
print(f'{i}...')
plot_expr(f'leak_{i}') |
I don't think I have the knowledge to track this down in the C layer (if that's indeed where it is), but FWIW this issue is present at least back to Matplotlib v3.2.2. |
Is the reference count the same with Qt backends? There is a reference held in the ObjC side on the |
Coming back to this, the growth I'm getting with the code in #19769 (comment) is
And the chain for the canvas is below. It looks like there might be an issue with |
This was fixed by: #23059 |
Bug report
Bug summary
Hi,
I believe I have found a memory issue when plotting multiple figures with the macOS backend.
Here is a minimal working example:
When running the code with

memory-profiler
, I obtain something like:Even though I am explicitly calling the
plt.close()
function.When using the

'agg'
backend instead of the'MacOSX'
backend by uncommenting line 6, I get:Which I would expect to be the normal behavior.
As I have been unable to reproduce this on Linux, I assume that the issue is with the macOS backend.
Matplotlib version
macOS 11.2.1
3.3.4
MacOSX
3.7.9
I installed Python via
venv
and matplotlib viapip
.Cheers,
Théotime
The text was updated successfully, but these errors were encountered: