Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: High memory usage in Figure.savefig #29434

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
zqianem opened this issue Jan 8, 2025 · 4 comments
Open

[Bug]: High memory usage in Figure.savefig #29434

zqianem opened this issue Jan 8, 2025 · 4 comments
Milestone

Comments

@zqianem
Copy link

zqianem commented Jan 8, 2025

Bug summary

When saving figures with large data images (imshow), matplotlib version 3.10.0 uses ~4.2 times the amount of memory, and runs ~2.5 times slower.

Code for reproduction

#!/usr/bin/env -S uv run --with humanize --with numpy --with "matplotlib==3.10"

import tracemalloc

import humanize
import matplotlib.pyplot as plt
import numpy as np

tracemalloc.start()

fig, ax = plt.subplots()
ax.imshow(np.zeros((10_000, 10_000)))

fig.savefig("test.png")

_, peak = tracemalloc.get_traced_memory()

print(f"Memory usage peaked at {humanize.naturalsize(peak)}")

Actual outcome

Memory usage peaked at 7.2 GB

Expected outcome

Memory usage peaked at 1.7 GB

This is the result when the shebang in the code example is changed to "matplotlib<3.10", or when the fig.savefig("test.png") line is removed.

Additional information

May be related to #28904, although switching the backend to Cairo doesn't affect the results.

Operating system

Arch

Matplotlib Version

3.10.0

Matplotlib Backend

agg

Python version

Python 3.13.0

Jupyter version

No response

Installation

None

@zqianem zqianem changed the title [Bug]: High memory usage by Figure.savefig starting in version 3.10.0 [Bug]: High memory usage by Figure.savefig Jan 8, 2025
@zqianem zqianem changed the title [Bug]: High memory usage by Figure.savefig [Bug]: High memory usage in Figure.savefig Jan 8, 2025
@QuLogic
Copy link
Member

QuLogic commented Jan 9, 2025

This bisects to 3ceba40; you can set image.interpolation_stage to data to avoid it switching to the rgba stage.

@zqianem
Copy link
Author

zqianem commented Jan 9, 2025

Thanks for tracking this down!

Would it be worth adding #28061, the image resampling docs, and this workaround to the 3.10 release notes? I could see this affecting quite of bit of code with non-obvious slowdowns and OOM errors (which is how I found it in the first place 🙂).

@tacaswell
Copy link
Member

Some what ironically, the big images are where the RGBA interpolation is probably the most useful.

@tacaswell tacaswell added this to the v3.10.1 milestone Jan 9, 2025
@jklymak
Copy link
Member

jklymak commented Jan 9, 2025

Yeah, I'm not quite sure why that is not in the release notes. Sorry for not catching that it was missing.

@ksunden ksunden modified the milestones: v3.10.1, v3.10.2 Feb 21, 2025
@ksunden ksunden modified the milestones: v3.10.2, v3.10.3 May 2, 2025
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

5 participants