FIX: squash memory leak in colorbar by jklymak · Pull Request #22089 · matplotlib/matplotlib · GitHub
Skip to content

FIX: squash memory leak in colorbar - #22089

Merged
tacaswell merged 2 commits into
matplotlib:mainfrom
jklymak:fix-memleak-make-colorbar
Jan 4, 2022
Merged

FIX: squash memory leak in colorbar#22089
tacaswell merged 2 commits into
matplotlib:mainfrom
jklymak:fix-memleak-make-colorbar

Conversation

@jklymak

@jklymak jklymak commented Jan 3, 2022

Copy link
Copy Markdown
Member

PR Summary

Closes #22085

I guess I'm not 100% sure why the old code breaks, but this new code does not leak memory.

Tested via

import matplotlib.pyplot as plt
from matplotlib import colorbar
import psutil
import gc
import numpy as np

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

Before:


66.297856
60.760064
...
86.355968
87.8592

Now:

(OK, there actually appears to be a slow memory leak somewhere else, but it is far slower than this memory leak...)

55.66464
51.818496
...
57.421824
57.63072

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@jklymak jklymak added this to the v3.5.2 milestone Jan 3, 2022
Comment thread lib/matplotlib/colorbar.py
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
@tacaswell
tacaswell merged commit 61fa334 into matplotlib:main Jan 4, 2022
@lumberbot-app

lumberbot-app Bot commented Jan 4, 2022

Copy link
Copy Markdown

tacaswell added a commit to tacaswell/matplotlib that referenced this pull request Jan 4, 2022
Merge pull request matplotlib#22089 from jklymak/fix-memleak-make-colorbar

FIX: squash memory leak in colorbar
QuLogic added a commit that referenced this pull request Jan 4, 2022
…-v3.5.x

Backport PR #22089: FIX: squash memory leak in colorbar
@jklymak
jklymak deleted the fix-memleak-make-colorbar branch November 9, 2022 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Memory leak with colorbar.make_axes

3 participants