Title: memory leak - simple function call in a loop · Issue #541 · pythonnet/pythonnet · GitHub
Open Graph Title: memory leak - simple function call in a loop · Issue #541 · pythonnet/pythonnet
X Title: memory leak - simple function call in a loop · Issue #541 · pythonnet/pythonnet
Description: I'm getting a memory leak with simple calls from .NET to Python function. The issue is reproducible on all pythonnet versions that I tested. When I test Python calls to .NET, then the memory leak is less obvious, since all resources are ...
Open Graph Description: I'm getting a memory leak with simple calls from .NET to Python function. The issue is reproducible on all pythonnet versions that I tested. When I test Python calls to .NET, then the memory leak i...
X Description: I'm getting a memory leak with simple calls from .NET to Python function. The issue is reproducible on all pythonnet versions that I tested. When I test Python calls to .NET, then the memory le...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/541
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"memory leak - simple function call in a loop","articleBody":"I'm getting a memory leak with simple calls from .NET to Python function. The issue is reproducible on all pythonnet versions that I tested. When I test Python calls to .NET, then the memory leak is less obvious, since all resources are released when calling the CPython `gc.collect()`. Calling the same function from .NET does not release any resources, but calling the CLR `GC.WaitForPendingFinalizers()` blocks the execution.\r\n\r\n[memoryleakpykw.zip](https://github.com/pythonnet/pythonnet/files/1293771/memoryleakpykw.zip)\r\n\r\n\r\n```python\r\n\u003e\u003e\u003e import clr\r\n\u003e\u003e\u003e import System\r\n\u003e\u003e\u003e for i in range(20000000):\r\n... a=System.Double(i)\r\n...\r\n\u003e\u003e\u003e import gc\r\n\u003e\u003e\u003e gc.collect()\r\n```\r\n\r\n`pyinclr.py`\r\n```python\r\ndef testmethod():\r\n\treturn \"abc\"*3\r\n```\r\n\r\n```csharp\r\nusing System;\r\nusing Python.Runtime;\r\n\r\nnamespace memoryleakpykw\r\n{\r\n class Program\r\n {\r\n static void Main(string[] args)\r\n {\r\n PyObject pymodule;\r\n PyObject gc;\r\n PyObject testmethod;\r\n\r\n PythonEngine.Initialize();\r\n\r\n IntPtr pylock = PythonEngine.AcquireLock();\r\n {\r\n PyObject sys = PythonEngine.ImportModule(\"sys\");// Py.Import(\"sys\");\r\n gc = PythonEngine.ImportModule(\"gc\");// Py.Import(\"sys\");\r\n sys.GetAttr(\"path\").InvokeMethod(\"append\",\r\n new PyString(@\"C:\\Users\\denis.akhiyarov\\source\\repos\\memoryleakpykw\\pyinclr\"));\r\n pymodule = PythonEngine.ImportModule(\"pyinclr\");\r\n testmethod = pymodule.GetAttr(\"testmethod\");\r\n }\r\n\r\n PythonEngine.ReleaseLock(pylock);\r\n\r\n Console.WriteLine(\"starting test for memory leak, press any key\");\r\n Console.ReadKey();\r\n\r\n pylock = PythonEngine.AcquireLock();\r\n\r\n {\r\n for (int i=1; i\u003c=1000000; i++)\r\n {\r\n testmethod.Invoke();\r\n }\r\n }\r\n\r\n gc.InvokeMethod(\"collect\"); \r\n\r\n PythonEngine.ReleaseLock(pylock);\r\n \r\n GC.Collect();\r\n //GC.WaitForPendingFinalizers(); // this blocks execution\r\n GC.Collect();\r\n\r\n Console.WriteLine(\"finished test for memory leak, press any key\");\r\n Console.ReadKey();\r\n }\r\n\r\n }\r\n}\r\n```\r\n\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/den-run-ai","@type":"Person","name":"den-run-ai"},"datePublished":"2017-09-11T19:32:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/541/pythonnet/issues/541"}
| 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:c2e6a5e1-0813-f617-c5e6-cf06bef36b90 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AD5A:11F8A0:48F7874:63208BF:69730550 |
| html-safe-nonce | 4ef9f7d71d19c9e29f2e56a0e4a35e4eaf8215ecdefd7fa0c76eb4f4d6bd284f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRDVBOjExRjhBMDo0OEY3ODc0OjYzMjA4QkY6Njk3MzA1NTAiLCJ2aXNpdG9yX2lkIjoiODczODY3NjAyNzMwMDkxODU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1999220fbdd00b31e2056b2378d9dbc39fa54dcf398bddd6fb56ae144e45bbb8 |
| hovercard-subject-tag | issue:256816591 |
| 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/541/issue_layout |
| twitter:image | https://opengraph.githubassets.com/681777193dac58b8549c716c8110fe7ec93e4a96cb0f8e750586b81186e27461/pythonnet/pythonnet/issues/541 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/681777193dac58b8549c716c8110fe7ec93e4a96cb0f8e750586b81186e27461/pythonnet/pythonnet/issues/541 |
| og:image:alt | I'm getting a memory leak with simple calls from .NET to Python function. The issue is reproducible on all pythonnet versions that I tested. When I test Python calls to .NET, then the memory leak i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | den-run-ai |
| hostname | github.com |
| expected-hostname | github.com |
| None | 44ab3188c1dcfe3be0f9c3feca2e04e14fb79f120939ce2395e4f15ab96ec1d4 |
| 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 | a5e2b48bd1260476599758f5d253b5d24092ab84 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width