René's URL Explorer Experiment


Title: [Bug]: Memory not freed as expected after plotting heavy plot involving looping · Issue #27138 · matplotlib/matplotlib · GitHub

Open Graph Title: [Bug]: Memory not freed as expected after plotting heavy plot involving looping · Issue #27138 · matplotlib/matplotlib

X Title: [Bug]: Memory not freed as expected after plotting heavy plot involving looping · Issue #27138 · matplotlib/matplotlib

Description: Solution #27138 (comment) Bug summary I work with a large 1D dataset. For a statistical analysis, I am using the bootstrap method, resampling it many times. I am interested in looping over all cases in order to put together on a single f...

Open Graph Description: Solution #27138 (comment) Bug summary I work with a large 1D dataset. For a statistical analysis, I am using the bootstrap method, resampling it many times. I am interested in looping over all case...

X Description: Solution #27138 (comment) Bug summary I work with a large 1D dataset. For a statistical analysis, I am using the bootstrap method, resampling it many times. I am interested in looping over all case...

Opengraph URL: https://github.com/matplotlib/matplotlib/issues/27138

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Memory not freed as expected after plotting heavy plot involving looping","articleBody":"### Solution\r\nhttps://github.com/matplotlib/matplotlib/issues/27138#issuecomment-1771141850\r\n\r\n### Bug summary\r\n\r\nI work with a large 1D dataset. For a statistical analysis, I am using the bootstrap method, resampling it many times.\r\n\r\nI am interested in **looping over all cases** in order to put together on a single figure a specific result for all resamplings. \r\n**Memory issues take place though (e.g. not freed before the very end of the script, or even leaks).**\r\n\r\nHere I document some things that at least partially address the issue. None is fully satisfactory though. \r\nI am running the same script both from Python and as a Jupyter notebook (synchronised via jupytext). I am trying to get rid of the memory issues in both cases (the RAM usage easily reaches 16–32 GB once I start playing with enough data).\r\n\r\n### Code for reproduction\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nda = np.sort(np.random.random(int(3e6))) # Beware: please lower this if your system has less than 32 GB\r\n\r\ndef custom_plot(da, **kwargs):\r\n    \"\"\"da is a 1-D ordered xarray.DataArray or numpy array (containing tons of data)\"\"\"\r\n    plt.yscale('log')\r\n    n = len(da)\r\n    return plt.plot(da, 1 - (np.arange(1, n+1) / (n+1)), **kwargs);\r\n\r\ndef resampling_method(da, case):\r\n    \"\"\"\r\n    A complex thing in reality but for the MWE, let us simply return da itself.\r\n    It will lead to the same memory problem.\r\n    \"\"\"\r\n    return da\r\n\r\nplt.figure(figsize=(15, 8), dpi=150)\r\nplt.ylim((1e-10,1))\r\n\r\nfor case in np.arange(50):\r\n    custom_plot(resampling_method(da, case)) # each time getting the curve for a different resampling of da\r\ncustom_plot(da) # curve for the original da\r\n\r\nplt.savefig(\"output.png\")\r\nplt.show()\r\n\r\nimport gc\r\ngc.collect();\r\n\r\nprint(\"Technically the programme would continue with more calculations.\")\r\nprint(\"Notice how the memory won't be freed however until the entire script is finished.\")\r\nimport time\r\ntime.sleep(120) # This simulates the fact that the programme continues with other calculations.\r\nprint(\"Now the programme exits\")\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\nMemory issues are taking place no matter what I've tried so far. Depending on what is being attempted, it can lead to the memory either not being freed after the plot has been shown/is closed, or even memory leaks and massive swap usage.\r\n\r\n### Expected outcome\r\n\r\nMemory freed well before the end of the programme. I would expect it to be freed soon after the figure is closed.\r\n\r\n### Additional information\r\n\r\nNB: I did also try many other things (incl. `plt.cla` and the like), as well as changing backend (notably \"Agg\" and \"Qt5Agg\") but that did not solve the problem in the slightest, so I won't document them.\r\n\r\n#### Things that have some effect\r\n\r\n1) If you do `plt.show()`\r\n- It will show the plot as a new window when run from the terminal with Python but the memory usage related to the figure won't be freed after that. It will remain in use until the end of the entire script.\r\n- it will be freed in Jupyter soon after displaying the figure however.\r\n```python\r\nplt.show()\r\nimport gc\r\ngc.collect();\r\n```\r\n\r\n2) If you use `block=False`, `time.sleep` and `close('all')`, the memory will be freed after the plot has been created both with Jupyter and Python. However, in Python, a window will be created (stealing focus) and nothing will ever appear in it (it will be closed after 5 seconds). It'd therefore be tempting to comment out `plt.show(block=False)` but if you do, Jupyter will no longer clear the memory...\r\n```python\r\nplt.show(block=False) ## If you comment this out, then Jupyter will not clear the memory..\r\nimport time\r\ntime.sleep(5)\r\nplt.close('all')\r\nimport gc\r\ngc.collect();\r\n```\r\n\r\n3) Given what precedes, let us check whether Jupyter or Python is being used.\r\n```python\r\n\r\ndef type_of_script():\r\n    \"\"\"source: https://stackoverflow.com/a/47428575/452522\"\"\"\r\n    try:\r\n        ipy_str = str(type(get_ipython()))\r\n        if 'zmqshell' in ipy_str:\r\n            return 'jupyter'\r\n        if 'terminal' in ipy_str:\r\n            return 'ipython'\r\n    except:\r\n        return 'terminal'\r\n\r\nif type_of_script() == \"jupyter\":\r\n    plt.show(block=False) ## If you comment this out, then Jupyter will not clear the memory..\r\nelse:\r\n    pass\r\nimport time\r\ntime.sleep(5)\r\nplt.close('all')\r\nimport gc\r\ngc.collect();\r\n```\r\nWith this:\r\n- Jupyter will create a file and will also display the figure inline.\r\n- Python will only create a file and won't try to show a window\r\nBoth will clean the memory after that figure has been closed (or 5 seconds after rather).\r\n**This is the most satisfactory one... not exactly nice though.**\r\nShould one want to have the figure displayed when running python from CLI however, I haven't found a method\r\nwhere the memory wouldn't remain in use until the very end of the entire script.\r\n\r\n#### Some further notes:\r\n- There are known memory issues with matplotlib and looping, such as: http://datasideoflife.com/?p=1443\r\nbut here I do not create a at each iteration of a loop, but accumulate plots from a loop and plot the end result. The solution put forward there (i.e. use `plt.close(fig)` does not work in this case).\r\n\r\n- This is also distinct from https://github.com/matplotlib/matplotlib/issues/20300\r\n\r\n### Operating system\r\n\r\nUbuntu\r\n\r\n### Matplotlib Version\r\n\r\n3.7.3\r\n\r\n### Matplotlib Backend\r\n\r\nmodule://matplotlib_inline.backend_inline (default)\r\n\r\n### Python version\r\n\r\n3.8.10\r\n\r\n### Jupyter version\r\n\r\n6.5.2\r\n\r\n### Installation\r\n\r\npip","author":{"url":"https://github.com/spacescientist","@type":"Person","name":"spacescientist"},"datePublished":"2023-10-18T12:47:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":22},"url":"https://github.com/27138/matplotlib/issues/27138"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f116d65d-f3e4-12d9-5b73-0d0888b1dd11
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idED96:9B27A:198478A:21A3581:6A51D40C
html-safe-nonceb1d1711093d03e17678be0d6aa3c9bfbfe0f71c18f55fd2a9d89253365cf673d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDk2OjlCMjdBOjE5ODQ3OEE6MjFBMzU4MTo2QTUxRDQwQyIsInZpc2l0b3JfaWQiOiI3Mzk4MzE4NTczNDYxMDMzOTk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacee7870d571b742744924d37155284b45151aa92fc14c4b41cb49b20c24f73764
hovercard-subject-tagissue:1949672523
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/27138/issue_layout
twitter:imagehttps://opengraph.githubassets.com/766a214a659144f5dcc2f704bb42e9f7b3910f552a3e45b5915bd9e540b2b03f/matplotlib/matplotlib/issues/27138
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/766a214a659144f5dcc2f704bb42e9f7b3910f552a3e45b5915bd9e540b2b03f/matplotlib/matplotlib/issues/27138
og:image:altSolution #27138 (comment) Bug summary I work with a large 1D dataset. For a statistical analysis, I am using the bootstrap method, resampling it many times. I am interested in looping over all case...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamespacescientist
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/27138#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F27138
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%2F27138
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/27138
Reloadhttps://github.com/matplotlib/matplotlib/issues/27138
Reloadhttps://github.com/matplotlib/matplotlib/issues/27138
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/27138
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/27138
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 409 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
[Bug]: Memory not freed as expected after plotting heavy plot involving loopinghttps://github.com/matplotlib/matplotlib/issues/27138#top
Community supportUsers in need of help.https://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22Community%20support%22
https://github.com/spacescientist
spacescientisthttps://github.com/spacescientist
on Oct 18, 2023https://github.com/matplotlib/matplotlib/issues/27138#issue-1949672523
#27138 (comment)https://github.com/matplotlib/matplotlib/issues/27138#issuecomment-1771141850
http://datasideoflife.com/?p=1443http://datasideoflife.com/?p=1443
Memory leak in plt.close() when unshown figures in GUI backends are closed #20300https://github.com/matplotlib/matplotlib/issues/20300
Community supportUsers in need of help.https://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22Community%20support%22
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.