Title: [Bug]: memory backing figure is not freed when figure is closed · Issue #29743 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: memory backing figure is not freed when figure is closed · Issue #29743 · matplotlib/matplotlib
X Title: [Bug]: memory backing figure is not freed when figure is closed · Issue #29743 · matplotlib/matplotlib
Description: Bug summary It seems that a reference to the figure object is kept in some global state. Executing the code below and closing the figures that pop up results in a linear increase in used memory. This happens with Matplotlib 3.9.1 and lat...
Open Graph Description: Bug summary It seems that a reference to the figure object is kept in some global state. Executing the code below and closing the figures that pop up results in a linear increase in used memory. Th...
X Description: Bug summary It seems that a reference to the figure object is kept in some global state. Executing the code below and closing the figures that pop up results in a linear increase in used memory. Th...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/29743
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: memory backing figure is not freed when figure is closed","articleBody":"### Bug summary\n\nIt seems that a reference to the figure object is kept in some global state. Executing the code below and closing the figures that pop up results in a linear increase in used memory. This happens with Matplotlib 3.9.1 and later. The latest release that does not exhibit the problem is Matplotlib 3.8.4.\n\nAdding `fig.clf()` after the `plt.show()` in the code below (clearing the figure after it has been displayed and closed by the user) results in the memory leak being significantly reduced, but not eliminated.\n\n### Code for reproduction\n\n```Python\nimport gc\nimport psutil\nimport numpy as np\nimport matplotlib.pyplot as plt\n\np = psutil.Process()\n\nd = np.linspace(0, 1, 1_000_000)\n\nfor i in range(10):\n fig, ax = plt.subplots()\n ax.plot(d)\n plt.show()\n gc.collect()\n print(p.memory_info().rss)\n```\n\n### Actual outcome\n\n185729024\n254550016\n321282048\n389459968\n456253440\n524554240\n592330752\n659394560\n727457792\n794468352\n\n### Expected outcome\n\n183324672\n251961344\n253427712\n253464576\n253493248\n254521344\n254529536\n253464576\n253468672\n254476288\n\n### Additional information\n\nThe leak has been introduced sometime between version 3.8.4 (which gives the expected outcome output above) and version 3.9.1 (which gives the actual outcome output above). It reproduces with Matplotlib version 3.9.4 and 3.10.1\n\n### Operating system\n\nWindows\n\n### Matplotlib Version\n\n3.9.1\n\n### Matplotlib Backend\n\nqtagg\n\n### Python version\n\n3.12.9\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\nconda","author":{"url":"https://github.com/dnicolodi","@type":"Person","name":"dnicolodi"},"datePublished":"2025-03-12T13:36:08.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/29743/matplotlib/issues/29743"}
| 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:a7a97cd8-273d-55d8-f179-b16b3295b015 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 810E:17F12E:211C30:2B9BFE:6A52C5C0 |
| html-safe-nonce | 58bd9170f7cce5c552f68eca19f897add9bb719579ba6a96bb9b4188c907c62e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTBFOjE3RjEyRToyMTFDMzA6MkI5QkZFOjZBNTJDNUMwIiwidmlzaXRvcl9pZCI6IjgwMzQ4OTA2OTU5MDE4ODk5ODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f50198f63ba5a8ccf5727d443289073e1ab425045c7a18874262e4cadae97a88 |
| hovercard-subject-tag | issue:2914003950 |
| 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/29743/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25d2d9e18eb1b7fba1ec99b3f88f64139a91943e86dea22cf9cc1afa8db56a00/matplotlib/matplotlib/issues/29743 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25d2d9e18eb1b7fba1ec99b3f88f64139a91943e86dea22cf9cc1afa8db56a00/matplotlib/matplotlib/issues/29743 |
| og:image:alt | Bug summary It seems that a reference to the figure object is kept in some global state. Executing the code below and closing the figures that pop up results in a linear increase in used memory. Th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dnicolodi |
| 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