[Bug]: High memory usage in `Figure.savefig` · Issue #29434 · matplotlib/matplotlib · GitHub
Skip to content

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

Description

@zqianem

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

Activity

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions