Title: Deleted dotnet object does not get garbage collected · Issue #1872 · pythonnet/pythonnet · GitHub
Open Graph Title: Deleted dotnet object does not get garbage collected · Issue #1872 · pythonnet/pythonnet
X Title: Deleted dotnet object does not get garbage collected · Issue #1872 · pythonnet/pythonnet
Description: Environment Pythonnet version: 3.0.0rc3 Python version: 3.10.5 Operating System: Windows 10 20H2 .NET Runtime: 6.0.6 Details Describe what you were trying to get done. Create dotnet-object and delete it. If I do that a lot I get memory i...
Open Graph Description: Environment Pythonnet version: 3.0.0rc3 Python version: 3.10.5 Operating System: Windows 10 20H2 .NET Runtime: 6.0.6 Details Describe what you were trying to get done. Create dotnet-object and dele...
X Description: Environment Pythonnet version: 3.0.0rc3 Python version: 3.10.5 Operating System: Windows 10 20H2 .NET Runtime: 6.0.6 Details Describe what you were trying to get done. Create dotnet-object and dele...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/1872
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deleted dotnet object does not get garbage collected","articleBody":"### Environment\r\n\r\n- Pythonnet version: 3.0.0rc3\r\n- Python version: 3.10.5\r\n- Operating System: Windows 10 20H2\r\n- .NET Runtime: 6.0.6\r\n\r\n### Details\r\n\r\n- Describe what you were trying to get done.\r\n\r\nCreate dotnet-object and delete it. If I do that a lot I get memory issues. It seems the never get cleared by the Python- or dotnet-gc. This is not a problem in pythonnet 2.5.2\r\n\r\n- What commands did you run to trigger this issue?\r\n\r\n```python\r\nimport gc\r\nimport clr\r\nimport psutil\r\nimport matplotlib.pyplot as plt\r\nimport System\r\n\r\n# Create dict store count of objects left\r\ntypes = [\r\n str,\r\n System.String,\r\n]\r\ntypes = {obj: 0 for obj in types}\r\n\r\nN = 1_000\r\nstr_size = 1_000_000\r\n\r\nmemory_py = [0 for _ in range(N + 2)]\r\nmemory_py[0] = psutil.virtual_memory().used\r\nfor ii in range(N):\r\n s_py = str('a' * str_size)\r\n del s_py\r\n memory_py[ii + 1] = psutil.virtual_memory().used - memory_py[0]\r\n\r\ngc.collect()\r\n# See what types of objects are still present\r\nfor obj in gc.get_objects():\r\n if type(obj) in list(types.keys()):\r\n types[type(obj)] += 1\r\nprint(types)\r\nmemory_py[ii + 2] = psutil.virtual_memory().used - memory_py[0]\r\nmemory_py[0] = 0\r\n```\r\n```\r\n{\u003cclass 'str'\u003e: 0, \u003cclass 'System.String'\u003e: 0}\r\n```\r\n```python\r\nmemory_net = [0 for _ in range(N + 2)]\r\nmemory_net[0] = psutil.virtual_memory().used\r\ns_py = str('a' * str_size)\r\nfor ii in range(N):\r\n s_net = System.String(s_py)\r\n del s_net\r\n memory_net[ii + 1] = psutil.virtual_memory().used - memory_net[0]\r\n\r\ngc.collect()\r\n# See what types of objects are still present\r\nfor obj in gc.get_objects():\r\n if type(obj) in list(types.keys()):\r\n types[type(obj)] += 1\r\nprint(types)\r\nmemory_net[ii + 2] = psutil.virtual_memory().used - memory_net[0]\r\nmemory_net[0] = 0\r\n```\r\n```\r\n{\u003cclass 'str'\u003e: 0, \u003cclass 'System.String'\u003e: 1000}\r\n```\r\nYou can also visualize the memory consumption:\r\n```python\r\nfig, ax = plt.subplots()\r\nax.plot(memory_py, label='Python object')\r\nax.plot(memory_net, label='DotNet object')\r\nax.legend()\r\nplt.show(fig)\r\n```\r\n\r\n\r\nYou can execute the same code with pythonnet 2.5.2 (Python 3.9.13) and get the following figure\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/m-rossi","@type":"Person","name":"m-rossi"},"datePublished":"2022-07-13T15:12:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/1872/pythonnet/issues/1872"}
| 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:5e2d2a37-fbbe-411b-f87e-f99498d95385 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8120:19E8FE:151A472:1C4EE28:69706824 |
| html-safe-nonce | 4c952b6eea8fe3316e84c97db345b3e3bfcf93ce4534d5fd1170d40001ae4fff |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTIwOjE5RThGRToxNTFBNDcyOjFDNEVFMjg6Njk3MDY4MjQiLCJ2aXNpdG9yX2lkIjoiMjMzNjE1MDU1NTA4MTA3NDcyNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b3c74a4af4f1cb999d8a98c6f88251b8ffcbd5c0de88e62326acfb8d4c37a664 |
| hovercard-subject-tag | issue:1303582921 |
| 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/pythonnet/pythonnet/1872/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d0dbe0714ce9f9e5bce0ff496c797f6d5babe69eee89034c1a05d3640f6a901/pythonnet/pythonnet/issues/1872 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d0dbe0714ce9f9e5bce0ff496c797f6d5babe69eee89034c1a05d3640f6a901/pythonnet/pythonnet/issues/1872 |
| og:image:alt | Environment Pythonnet version: 3.0.0rc3 Python version: 3.10.5 Operating System: Windows 10 20H2 .NET Runtime: 6.0.6 Details Describe what you were trying to get done. Create dotnet-object and dele... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | m-rossi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9920a62ba22d06470388e2904804fb7e5ec51c9e35f81784e9191394c74b2bd2 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| 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 | 7d6181066430cc06553c8396ca201e194ae33cb9 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width