Skip to content

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

Open
@zqianem

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions