Skip to content

[Bug]: GPU memory leak with plt.figure() #22448

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

Open
patrickhaoy opened this issue Feb 11, 2022 · 9 comments
Open

[Bug]: GPU memory leak with plt.figure() #22448

patrickhaoy opened this issue Feb 11, 2022 · 9 comments

Comments

@patrickhaoy
Copy link

Bug summary

There seems to be a GPU memory leak when calling plt.figure() and plt.close() repeatedly in matplotlib version 3.5.0. Every time these two lines are ran, more and more GPU memory gets hogged up.

Code for reproduction

from matplotlib import pyplot as plt

for i in range(10000):
    plt.figure()
    plt.close()

Actual outcome

When inspecting memory usage, more and more GPU memory gets hogged up. Specifically, I ran nvidia-smi -l 1 and observed Memory-Usage increasing until it hits the GPU memory limits, which is when the process crashes.

Expected outcome

GPU memory usage should not be going up every iteration of this for loop.

Additional information

This is an issue in matplotlib version 3.5.0, but not 3.0.3.

Operating system

Ubuntu

Matplotlib Version

3.5.0

Matplotlib Backend

TkAgg

Python version

3.9.10

Jupyter version

6.4.8

Installation

pip

@tacaswell
Copy link
Member

I have no idea how we are leaking to the GPU! That is definitely new.

However, I think that this is the same issue we have had with tk with system memory. In plt.figure() we are creating a bunch of Tk objects, then throwing away the Python side objects, telling the c++ side objects they can clean up, but never running the Tk event loop to give them a chance to. I am not sure that this is really a leak as I suspect that if you were to run the Tk event loop the memory would be relaesed, but it is never getting a chance.

Either using

mpl.use('agg')

if you never need the GUI windows or making sure you actually show each window to screen before you close it will fix the memory issue.

@patrickhaoy
Copy link
Author

Got it, thank you!

@tacaswell
Copy link
Member

tacaswell commented Feb 11, 2022

I also believe @richardsheridan has a plan on how to fix this.

@richardsheridan
Copy link
Contributor

You are probably referring to #22002? @patrickhaoy could you install from that pr and see if it fixes your leak?

@patrickhaoy
Copy link
Author

I installed from your maybe_update branch, but the memory leak still occurs.

@QuLogic
Copy link
Member

QuLogic commented Apr 20, 2022

Can you try again from #22002, as I think @richardsheridan found an actual big leak with the window_dpi thing.

@patrickhaoy
Copy link
Author

I installed matplotlib from #22002 again, but the memory leak still occurs.

@richardsheridan
Copy link
Contributor

Thanks for checking. This along with the rss measurement basically confirms we are leaking non-python objects still, but I still can't see from where. I'm looking forward to throwing memray at it.

@nschloe
Copy link
Contributor

nschloe commented Aug 21, 2022

I have this memory leak, but in regular old RAM with mpl 3.5.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants