Title: [Bug]: High memory usage in `Figure.savefig` · Issue #29434 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: High memory usage in `Figure.savefig` · Issue #29434 · matplotlib/matplotlib
X Title: [Bug]: High memory usage in `Figure.savefig` · Issue #29434 · matplotlib/matplotlib
Description: 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 --wi...
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/29434
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: High memory usage in `Figure.savefig`","articleBody":"### Bug summary\r\n\r\nWhen 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.\r\n\r\n### Code for reproduction\r\n\r\n```Python\r\n#!/usr/bin/env -S uv run --with humanize --with numpy --with \"matplotlib==3.10\"\r\n\r\nimport tracemalloc\r\n\r\nimport humanize\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\ntracemalloc.start()\r\n\r\nfig, ax = plt.subplots()\r\nax.imshow(np.zeros((10_000, 10_000)))\r\n\r\nfig.savefig(\"test.png\")\r\n\r\n_, peak = tracemalloc.get_traced_memory()\r\n\r\nprint(f\"Memory usage peaked at {humanize.naturalsize(peak)}\")\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\n```\r\nMemory usage peaked at 7.2 GB\r\n```\r\n\r\n### Expected outcome\r\n\r\n```\r\nMemory usage peaked at 1.7 GB\r\n```\r\n\r\nThis is the result when the shebang in the code example is changed to \"matplotlib\u003c3.10\", or when the `fig.savefig(\"test.png\")` line is removed.\r\n\r\n### Additional information\r\n\r\nMay be related to #28904, although switching the backend to Cairo doesn't affect the results.\r\n\r\n### Operating system\r\n\r\nArch\r\n\r\n### Matplotlib Version\r\n\r\n3.10.0\r\n\r\n### Matplotlib Backend\r\n\r\nagg\r\n\r\n### Python version\r\n\r\nPython 3.13.0\r\n\r\n### Jupyter version\r\n\r\n_No response_\r\n\r\n### Installation\r\n\r\nNone","author":{"url":"https://github.com/zqianem","@type":"Person","name":"zqianem"},"datePublished":"2025-01-08T21:18:05.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/29434/matplotlib/issues/29434"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ab1e4015-f33d-9773-9ce3-f102799b2da0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEB0:288515:957A5D:C6CB34:6A531462 |
| html-safe-nonce | 37d8fc4d2d758904140ec9bcb145677aca925a94577440e8034d872a0b9ee614 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUIwOjI4ODUxNTo5NTdBNUQ6QzZDQjM0OjZBNTMxNDYyIiwidmlzaXRvcl9pZCI6IjY4MzAzNDE5NTc4MjAyOTAxNDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 30f2cac424937c334adc8c992bc3f5fd56459545d866b9f2f18ae43b07b7ffe5 |
| hovercard-subject-tag | issue:2776367522 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/29434/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3349f0a46a7003f96bd74b4108e5a144d17f952cf9f6fa1effc390b257a72a0c/matplotlib/matplotlib/issues/29434 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3349f0a46a7003f96bd74b4108e5a144d17f952cf9f6fa1effc390b257a72a0c/matplotlib/matplotlib/issues/29434 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zqianem |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width