Title: [Bug]: plt.savefig leaks the stack · Issue #25406 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: plt.savefig leaks the stack · Issue #25406 · matplotlib/matplotlib
X Title: [Bug]: plt.savefig leaks the stack · Issue #25406 · matplotlib/matplotlib
Description: Bug summary Calling plt.savefig leaks local variables from parent calling functions. These can be found in gc.get_objects() and are cleared by gc.collect() Code for reproduction import gc import matplotlib.pyplot as plt class SomeObject:...
Open Graph Description: Bug summary Calling plt.savefig leaks local variables from parent calling functions. These can be found in gc.get_objects() and are cleared by gc.collect() Code for reproduction import gc import ma...
X Description: Bug summary Calling plt.savefig leaks local variables from parent calling functions. These can be found in gc.get_objects() and are cleared by gc.collect() Code for reproduction import gc import ma...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/25406
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: plt.savefig leaks the stack","articleBody":"### Bug summary\r\n\r\nCalling `plt.savefig` leaks local variables from parent calling functions. These can be found in `gc.get_objects()` and are cleared by `gc.collect()`\r\n\r\n### Code for reproduction\r\n\r\n```python\r\nimport gc\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nclass SomeObject:\r\n pass\r\n\r\ndef save_plots():\r\n plt.plot([1, 2, 3])\r\n plt.savefig(\"test.png\")\r\n\r\ndef leaky():\r\n some_object = SomeObject()\r\n save_plots()\r\n\r\nleaky()\r\n\r\nfor obj in gc.get_objects():\r\n try:\r\n if isinstance(obj, SomeObject):\r\n print(\"Object is leaking\")\r\n except:\r\n pass\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\n`Object is leaking`\r\n\r\n### Expected outcome\r\n\r\nNothing should be printed.\r\n\r\n### Additional information\r\n\r\n- I initially found this bug because I realized my deep learning training loop was leaking VRAM. Torch tensors were being kept in memory when `Figure.savefig` was called\r\n- I am under the impression that `ExitStack` within `savefig` is meant to hack around this issue but is somehow not working in my case?\r\n- I have been through issue #23701, but here I am able to provide a short and reproducible example\r\n- I have been through #20490 as well, but the issue persists with the `agg` backend\r\n- It is not acceptable to have to call `gc.collect()`. Most users won't be aware of the issue. Given the prevalence of deep learning these days, it's likely objects leaking into GPU memory will be a common problem due to this bug.\r\n\r\n### Operating system\r\n\r\nWin10 x64\r\n\r\n### Matplotlib Version\r\n\r\n3.7.1\r\n\r\n### Matplotlib Backend\r\n\r\nTkAgg\r\n\r\n### Python version\r\n\r\n3.10.0\r\n\r\n### Jupyter version\r\n\r\n_No response_\r\n\r\n### Installation\r\n\r\npip","author":{"url":"https://github.com/CorentinJ","@type":"Person","name":"CorentinJ"},"datePublished":"2023-03-07T13:11:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":24},"url":"https://github.com/25406/matplotlib/issues/25406"}
| 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:74853096-1fc7-56ef-414b-1a68e085b6a9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8830:2C3273:5450BF:70D3DF:6A519A4E |
| html-safe-nonce | 8e54ff9294641bbf401e06ad7ef18fcce2175040ae73ede1be39d15bed22dff3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODMwOjJDMzI3Mzo1NDUwQkY6NzBEM0RGOjZBNTE5QTRFIiwidmlzaXRvcl9pZCI6IjEwMjc4ODE5MzQ1MjM1NzA3NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ea546154e69e7a3d72d3bcb87ff68cac2766a34c458f21add9b54716f038f68e |
| hovercard-subject-tag | issue:1613414314 |
| 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/25406/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4e86b25d0df0a2f01086c284c53ad55481c3a4d9a3e79a2634890e8de4db6099/matplotlib/matplotlib/issues/25406 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4e86b25d0df0a2f01086c284c53ad55481c3a4d9a3e79a2634890e8de4db6099/matplotlib/matplotlib/issues/25406 |
| og:image:alt | Bug summary Calling plt.savefig leaks local variables from parent calling functions. These can be found in gc.get_objects() and are cleared by gc.collect() Code for reproduction import gc import ma... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CorentinJ |
| 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