Title: [Bug]: Megabyte-level memory leak when using imshow() in a loop · Issue #30400 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Megabyte-level memory leak when using imshow() in a loop · Issue #30400 · matplotlib/matplotlib
X Title: [Bug]: Megabyte-level memory leak when using imshow() in a loop · Issue #30400 · matplotlib/matplotlib
Description: Bug summary When creating, plotting to, and closing a figure in a scope (I tested with this in a function called in a loop), the memory usage steadily increases, sometimes by several megabytes for even simple calls to imshow. (p.s. in th...
Open Graph Description: Bug summary When creating, plotting to, and closing a figure in a scope (I tested with this in a function called in a loop), the memory usage steadily increases, sometimes by several megabytes for ...
X Description: Bug summary When creating, plotting to, and closing a figure in a scope (I tested with this in a function called in a loop), the memory usage steadily increases, sometimes by several megabytes for ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/30400
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Megabyte-level memory leak when using imshow() in a loop","articleBody":"### Bug summary\n\nWhen creating, plotting to, and closing a figure in a scope (I tested with this in a function called in a loop), the memory usage steadily increases, sometimes by several megabytes for even simple calls to imshow.\n\n(p.s. in the code below, the generate_frame function gets called 3 times to \"warm up\" the Python interpreter, just in case. same results with or without those lines, though)\n\n### Code for reproduction\n\n```Python\nimport gc\nimport numpy as np\nimport os\nimport psutil\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nmatplotlib.use('Agg')\n\ndef generate_frame(frame_idx: int):\n plt.style.use(\"dark_background\")\n fig, axes = plt.subplots(2, 3, figsize=(16, 8))\n axes = axes.flatten()\n\n for i in range(6):\n data = np.random.rand(1_000, 2_000)\n axes[i].imshow(data)\n\n plt.savefig(\"test.png\", dpi=400, bbox_inches='tight', pad_inches=0.1)\n\n plt.clf()\n plt.close('all')\n del fig\n gc.collect()\n return None\n\ndef get_memory():\n return psutil.Process(os.getpid()).memory_info().rss / (1024**2)\n\nfor _ in range(3):\n generate_frame(-1)\n\nprev = get_memory()\nfor i in range(0, 20):\n generate_frame(i)\n current = get_memory()\n print(f\"Generated frame {i + 1}. Delta: {current - prev:.4f} MB of memory.\")\n prev = current\n```\n\n### Actual outcome\n\n```\nGenerated frame 1. Delta: 4.1602 MB of memory.\nGenerated frame 2. Delta: 12.0195 MB of memory.\nGenerated frame 3. Delta: 0.2266 MB of memory.\nGenerated frame 4. Delta: 0.0859 MB of memory.\nGenerated frame 5. Delta: 4.1641 MB of memory.\nGenerated frame 6. Delta: 4.1758 MB of memory.\nGenerated frame 7. Delta: 0.0781 MB of memory.\nGenerated frame 8. Delta: 0.0430 MB of memory.\nGenerated frame 9. Delta: 0.0156 MB of memory.\nGenerated frame 10. Delta: 0.0117 MB of memory.\nGenerated frame 11. Delta: 0.1289 MB of memory.\nGenerated frame 12. Delta: 0.0117 MB of memory.\nGenerated frame 13. Delta: 0.3750 MB of memory.\nGenerated frame 14. Delta: 4.0859 MB of memory.\nGenerated frame 15. Delta: 4.1172 MB of memory.\nGenerated frame 16. Delta: 0.0234 MB of memory.\nGenerated frame 17. Delta: 0.0195 MB of memory.\nGenerated frame 18. Delta: 0.0195 MB of memory.\nGenerated frame 19. Delta: 0.0234 MB of memory.\nGenerated frame 20. Delta: 0.0234 MB of memory.\n```\n\n### Expected outcome\n\nUsing the same code but with no reference to matplotlib, gives:\n```\nGenerated frame 1. Delta: 4.0000 B of memory.\nGenerated frame 2. Delta: 12.0000 B of memory.\nGenerated frame 3. Delta: 176.0000 B of memory.\nGenerated frame 4. Delta: 504.0000 B of memory.\nGenerated frame 5. Delta: 164.0000 B of memory.\nGenerated frame 6. Delta: 0.0000 B of memory.\nGenerated frame 7. Delta: 160.0000 B of memory.\nGenerated frame 8. Delta: 0.0000 B of memory.\nGenerated frame 9. Delta: 28.0000 B of memory.\nGenerated frame 10. Delta: 16.0000 B of memory.\nGenerated frame 11. Delta: 0.0000 B of memory.\nGenerated frame 12. Delta: 24.0000 B of memory.\nGenerated frame 13. Delta: 24.0000 B of memory.\nGenerated frame 14. Delta: 0.0000 B of memory.\nGenerated frame 15. Delta: 0.0000 B of memory.\nGenerated frame 16. Delta: 0.0000 B of memory.\nGenerated frame 17. Delta: 0.0000 B of memory.\nGenerated frame 18. Delta: 0.0000 B of memory.\nGenerated frame 19. Delta: 0.0000 B of memory.\nGenerated frame 20. Delta: 0.0000 B of memory.\n```\n\nCode for that output:\n```py\nimport gc\nimport numpy as np\nimport os\nimport psutil\n\ndef generate_frame(frame_idx: int):\n for i in range(6):\n data = np.random.rand(100, 200)\n\n gc.collect()\n return None\n\ndef get_memory():\n return psutil.Process(os.getpid()).memory_info().rss / (1024)\n\nfor _ in range(3):\n generate_frame(-1)\n\nprev = get_memory()\nfor i in range(0, 20):\n generate_frame(i)\n current = get_memory()\n print(f\"Generated frame {i + 1}. Delta: {current - prev:.4f} B of memory.\")\n prev = current\n```\n\nIn other words, matplotlib should properly clean up references/resources to not generate a consistent upward trend in memory usage.\n\n### Additional information\n\nIn my real use-case, code very similar to this (but with colorbars. additional axes, etc.), using larger data, generates nearly 0.2 GB per frame as a consistent trend. I'm pretty sure the issue comes from matplotlib, as demonstrated with the example above.\n\n### Operating system\n\nMacOS\n\n### Matplotlib Version\n\n3.10.3\n\n### Matplotlib Backend\n\nAgg\n\n### Python version\n\n3.12.10\n\n### Jupyter version\n\nN/A\n\n### Installation\n\npip","author":{"url":"https://github.com/borisnezlobin","@type":"Person","name":"borisnezlobin"},"datePublished":"2025-08-06T23:23:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/30400/matplotlib/issues/30400"}
| 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:dec7c943-efe3-aba4-2325-f6a55c1b4f7b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A544:FAECF:2E848A:3E983E:6A525B26 |
| html-safe-nonce | 911aeba6108885b704c415e7f17c9ddd505a52e404980217eee1cf798106ae10 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTQ0OkZBRUNGOjJFODQ4QTozRTk4M0U6NkE1MjVCMjYiLCJ2aXNpdG9yX2lkIjoiNzE5NTQ4MTQ1ODY0MjkzNDU2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 388a7c245d9cc542a0a6aeaaa8ccda9e86aa5f77e505798f524c7c829333312a |
| hovercard-subject-tag | issue:3298368387 |
| 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/30400/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0b6cc54f272f7f168d2fbb90a7b30498fa9a3b49df16aae11b6f5590e792be8b/matplotlib/matplotlib/issues/30400 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0b6cc54f272f7f168d2fbb90a7b30498fa9a3b49df16aae11b6f5590e792be8b/matplotlib/matplotlib/issues/30400 |
| og:image:alt | Bug summary When creating, plotting to, and closing a figure in a scope (I tested with this in a function called in a loop), the memory usage steadily increases, sometimes by several megabytes for ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | borisnezlobin |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width