Skip to content
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

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

Closed
vdrhtc opened this issue Jan 2, 2022 · 7 comments · Fixed by #22089
Closed

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

vdrhtc opened this issue Jan 2, 2022 · 7 comments · Fixed by #22089

Comments

@vdrhtc
Copy link
Contributor

vdrhtc commented Jan 2, 2022

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)

@vdrhtc
Copy link
Contributor Author

vdrhtc commented Jan 2, 2022

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.

@jklymak
Copy link
Member

jklymak commented Jan 2, 2022

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.

@jklymak
Copy link
Member

jklymak commented Jan 3, 2022

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

@jklymak
Copy link
Member

jklymak commented Jan 3, 2022

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?

@vdrhtc
Copy link
Contributor Author

vdrhtc commented Jan 3, 2022

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

@jklymak
Copy link
Member

jklymak commented Jan 3, 2022

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.

@vdrhtc
Copy link
Contributor Author

vdrhtc commented Jan 3, 2022

@jklymak Thank you very much!

@QuLogic QuLogic added this to the v3.5.2 milestone Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants