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

[Bug]: Memory leak with colorbar.make_axes · Issue #22085 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Memory leak with colorbar.make_axes #22085

Description

Bug summary

When using matplotlib.colorbar.make_axes function, plt.close() does not clear the new colorbar axes from memory. Tested for the Qt5 backend only. Maybe related to #22002, at least in the sense that I see no leak for Qt5 when no colorbar axes are added.

Code for reproduction

%pylab qt5
from matplotlib import colorbar
import psutil
import gc

p = psutil.Process()
for i in range(5):
    fig, ax = plt.subplots(1,1)
    ax.imshow(random.normal(size=(100,100)))
    cax = colorbar.make_axes(ax)
    plt.savefig("test.png")
    plt.pause(0.1)
    plt.close(fig)
    del fig
    del ax
    del cax
    gc.collect()
    print(p.memory_full_info().uss/1e6)

Actual outcome

250.744832
255.209472
259.514368
264.13056
268.099584

Expected outcome

Constant memory usage, as is when the line
cax = colorbar.make_axes(ax)
is commented out.

Additional information

No response

Operating system

Windows

Matplotlib Version

3.4.2

Matplotlib Backend

Qt5Agg (PyQt)

Python version

3.7.6

Jupyter version

No response

Installation

from source (.tar.gz)

Activity

  1. vdrhtc commented on Jan 2, 2022

    ContributorAuthor

    Oh, also just found #22040, probably mine is a duplicate. The difference is that I do not yet know, how to workaround the problem in my example.

  2. jklymak commented on Jan 2, 2022

    Member

    Perhaps also test the latest matplotlib. There were substantial changes to colorbar for 3.5. FWIW, I don;t know what make_axes is supposed to do in your code.

  3. jklymak commented on Jan 3, 2022

    Member

    OK, I can confirm this when make_axes is used. When make_axes_gridspec is used, there is no memory leak.

  4. jklymak commented on Jan 3, 2022

    Member

    If I insert

    ...
        del ax 
        del cax._colorbar_info
        del cax
    ...

    then the memory leak goes away.

    This is defined in make_axes:

    cax._colorbar_info = dict(
    location=location,
    parents=parents,
    shrink=shrink,
    anchor=anchor,
    panchor=panchor,
    fraction=fraction,
    aspect=aspect0,
    pad=pad)

    and used by constrained_layout. I am not clear why adding a dictionary to an object would leave the dictionary orphaned when the parent is deleted?

  5. vdrhtc commented on Jan 3, 2022

    ContributorAuthor

    Yes, I found that grid spec helps, too, thank you!

  6. jklymak commented on Jan 3, 2022

    Member

    The culprit is parents = np.atleast_1d(parents).ravel(). If I get rid of this, no memory leak. However, this is needed, and I am not sure why it leaks.

  7. vdrhtc commented on Jan 3, 2022

    ContributorAuthor

    @jklymak Thank you very much!

  8. added this to the v3.5.2 milestone on Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL