FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: GPU memory leak with plt.figure() · Issue #22448 · matplotlib/matplotlib · GitHub

Repository navigation

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

Description

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

Activity

  1. tacaswell commented on Feb 11, 2022

    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.

  2. patrickhaoy commented on Feb 11, 2022

    Author

    Got it, thank you!

  3. tacaswell commented on Feb 11, 2022

    Member

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

  4. richardsheridan commented on Feb 11, 2022

    Contributor

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

  5. patrickhaoy commented on Feb 11, 2022

    Author

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

  6. QuLogic commented on Apr 20, 2022

    Member

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

  7. patrickhaoy commented on Apr 20, 2022

    Author

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

  8. richardsheridan commented on Apr 21, 2022

    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.

  9. nschloe commented on Aug 21, 2022

    Contributor

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

  10. github-actions commented on Dec 22, 2025

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  11. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: tkPerformancestatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL