René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:dec7c943-efe3-aba4-2325-f6a55c1b4f7b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA544:FAECF:2E848A:3E983E:6A525B26
html-safe-nonce911aeba6108885b704c415e7f17c9ddd505a52e404980217eee1cf798106ae10
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTQ0OkZBRUNGOjJFODQ4QTozRTk4M0U6NkE1MjVCMjYiLCJ2aXNpdG9yX2lkIjoiNzE5NTQ4MTQ1ODY0MjkzNDU2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac388a7c245d9cc542a0a6aeaaa8ccda9e86aa5f77e505798f524c7c829333312a
hovercard-subject-tagissue:3298368387
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/30400/issue_layout
twitter:imagehttps://opengraph.githubassets.com/0b6cc54f272f7f168d2fbb90a7b30498fa9a3b49df16aae11b6f5590e792be8b/matplotlib/matplotlib/issues/30400
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/0b6cc54f272f7f168d2fbb90a7b30498fa9a3b49df16aae11b6f5590e792be8b/matplotlib/matplotlib/issues/30400
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameborisnezlobin
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git
octolytics-dimension-user_id215947
octolytics-dimension-user_loginmatplotlib
octolytics-dimension-repository_id1385122
octolytics-dimension-repository_nwomatplotlib/matplotlib
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1385122
octolytics-dimension-repository_network_root_nwomatplotlib/matplotlib
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7aed05249554b889eb33d002851a973eebcc7e91
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/matplotlib/matplotlib/issues/30400#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F30400
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F30400
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=matplotlib%2Fmatplotlib
Reloadhttps://github.com/matplotlib/matplotlib/issues/30400
Reloadhttps://github.com/matplotlib/matplotlib/issues/30400
Reloadhttps://github.com/matplotlib/matplotlib/issues/30400
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/30400
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/30400
Notifications https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Fork 8.4k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Star 23k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Code https://github.com/matplotlib/matplotlib
Issues 1.1k https://github.com/matplotlib/matplotlib/issues
Pull requests 408 https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality 0 https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Code https://github.com/matplotlib/matplotlib
Issues https://github.com/matplotlib/matplotlib/issues
Pull requests https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
#28831https://github.com/matplotlib/matplotlib/pull/28831
[Bug]: Megabyte-level memory leak when using imshow() in a loophttps://github.com/matplotlib/matplotlib/issues/30400#top
#28831https://github.com/matplotlib/matplotlib/pull/28831
v3.11.0https://github.com/matplotlib/matplotlib/milestone/96
https://github.com/borisnezlobin
borisnezlobinhttps://github.com/borisnezlobin
on Aug 6, 2025https://github.com/matplotlib/matplotlib/issues/30400#issue-3298368387
v3.11.0https://github.com/matplotlib/matplotlib/milestone/96
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.