Title: apparent memory leak with live plotting · Issue #11956 · matplotlib/matplotlib · GitHub
Open Graph Title: apparent memory leak with live plotting · Issue #11956 · matplotlib/matplotlib
X Title: apparent memory leak with live plotting · Issue #11956 · matplotlib/matplotlib
Description: Bug report I've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the program. I'm continually updating my plot, but I a...
Open Graph Description: Bug report I've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the program. I...
X Description: Bug report I've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the progra...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/11956
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"apparent memory leak with live plotting","articleBody":"\u003c!--To help us understand and resolve your issue, please fill out the form to the best of your ability.--\u003e\r\n\u003c!--You can feel free to delete the sections that do not apply.--\u003e\r\n\r\n### Bug report\r\n\r\nI've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the program. I'm continually updating my plot, but I am only plotting the most recent 100 data points (so that the data itself isn't contributing to the memory increase). See an overly simplified example below that still shows the issue. As you will see when running the code below, matplotlib\\transforms.py just continually increases by around 5-15 kb on each turn of the loop, despite clearing my axes and using the object oriented version of matplotlib. This increase will continue to happen until it fills up the memory of my computer. Is this intentional? My thought was that in the object oriented program version of matplotlib, my code shouldn't increase in memory like this.\r\n\r\nMost of the code below is simply just tracemalloc helper functions for keeping track of memory.\r\n\r\n**Code for reproduction**\r\n\r\n\u003c!--A minimum code snippet required to reproduce the bug, also minimizing the number of dependencies required--\u003e\r\n\r\n```python\r\n\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport os\r\nimport linecache\r\nimport sys\r\nimport tracemalloc\r\nimport time\r\n\r\n\r\ndef display_top(snapshot, key_type='lineno', limit=2):\r\n '''\r\n function for pretty printing tracemalloc output\r\n '''\r\n snapshot = snapshot.filter_traces((\r\n tracemalloc.Filter(False, \"\u003cfrozen importlib._bootstrap\u003e\"),\r\n tracemalloc.Filter(False, \"\u003cunknown\u003e\"),\r\n ))\r\n top_stats = snapshot.statistics(key_type)\r\n\r\n print(\"Top %s lines\" % limit)\r\n for index, stat in enumerate(top_stats[:limit], 1):\r\n frame = stat.traceback[0]\r\n # replace \"/path/to/module/file.py\" with \"module/file.py\"\r\n filename = os.sep.join(frame.filename.split(os.sep)[-2:])\r\n print(\"#%s: %s:%s: %.1f KiB\"\r\n % (index, filename, frame.lineno, stat.size / 1024))\r\n line = linecache.getline(frame.filename, frame.lineno).strip()\r\n if line:\r\n print(' %s' % line)\r\n\r\n other = top_stats[limit:]\r\n if other:\r\n size = sum(stat.size for stat in other)\r\n print(\"%s other: %.1f KiB\" % (len(other), size / 1024))\r\n total = sum(stat.size for stat in top_stats)\r\n print(\"Total allocated size: %.1f KiB\" % (total / 1024))\r\n\r\ntracemalloc.start()\r\n\r\ny = np.random.rand(100)\r\nx = range(len(y))\r\n\r\n\r\nfig = plt.figure()\r\nax = fig.add_subplot(111)\r\nax.plot(x,y,'b-')\r\nplt.show(block=False)\r\n\r\nt0 = time.time()\r\n \r\nwhile True:\r\n try:\r\n ax.clear()\r\n ax.plot(x,np.random.rand(100),'b-')\r\n plt.pause(0.0001)\r\n snapshot = tracemalloc.take_snapshot()\r\n print(\"Run Time:\", time.time()-t0)\r\n display_top(snapshot)\r\n time.sleep(0.05)\r\n except KeyboardInterrupt:\r\n break\r\n\r\n```\r\n\r\nIf you'd prefer to use your own memory debugger, here's the simplest form of this issue:\r\n\r\n```python\r\n\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport time\r\ny = np.random.rand(100)\r\nx = range(len(y))\r\n\r\nfig = plt.figure()\r\nax = fig.add_subplot(111)\r\nax.plot(x,y,'b-')\r\nplt.show(block=False)\r\n\r\nt0 = time.time()\r\n \r\nwhile True:\r\n try:\r\n ax.clear()\r\n ax.plot(x,np.random.rand(100),'b-')\r\n plt.pause(0.0001)\r\n print(\"Run Time:\", time.time()-t0)\r\n time.sleep(0.05)\r\n except KeyboardInterrupt:\r\n break\r\n```\r\n\r\n\r\nIn the console output, you'll see matplotlib\\transforms.py quickly become the largest memory holder, and increase continually.\r\n\r\nThis issue also appears if I use a line object and line.set_xdata, etc., rather than ax.plot().\r\n\r\nUsing WinPython, Python 3.6.2\r\nMatplotlib version 2.2.2\r\nWindows 7 and Windows 10\r\nBackend TkAgg\r\n\r\n","author":{"url":"https://github.com/mgeorgiadis","@type":"Person","name":"mgeorgiadis"},"datePublished":"2018-08-28T14:08:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/11956/matplotlib/issues/11956"}
| 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:87faee7b-01e5-4f92-55de-94f472fa7117 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8BC:206F88:40AC98D:59B6803:6A54D565 |
| html-safe-nonce | 17ee0839add387c7008e199221352ae6a6bf32ded547e3798c2265f8f97bed18 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEJDOjIwNkY4ODo0MEFDOThEOjU5QjY4MDM6NkE1NEQ1NjUiLCJ2aXNpdG9yX2lkIjoiNzkyMTM5MDMyNjc2OTgzMzMxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 51a8dfd20cc456f2fb9c3e2f40e87accee84eb3b9ab7d2a477fe94b7fc65c6ca |
| hovercard-subject-tag | issue:354748860 |
| 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/11956/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6a91dbaab8cfcaed4835db7197aaa50e53b3aa4227c4fcb7ef30441c8b3feb2/matplotlib/matplotlib/issues/11956 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6a91dbaab8cfcaed4835db7197aaa50e53b3aa4227c4fcb7ef30441c8b3feb2/matplotlib/matplotlib/issues/11956 |
| og:image:alt | Bug report I've noticed that when running a matplotlib as a run-chart/long term plotting tool, memory use by matplotlib increases over the course of about a week, ultimately crashing the program. I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mgeorgiadis |
| hostname | github.com |
| expected-hostname | github.com |
| None | a556215b071af6609619e2bbe6f00bdfbf0812ad723b1ae6c301858a7a829f54 |
| 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 | 840b5d20d5a883c23cf56f5240b2c343d6cc0867 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width