- Notifications
You must be signed in to change notification settings - Fork752
Closed
Labels
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 released when calling the CPythongc.collect()
. Calling the same function from .NET does not release any resources, but calling the CLRGC.WaitForPendingFinalizers()
blocks the execution.
>>>importclr>>>importSystem>>>foriinrange(20000000):...a=System.Double(i)...>>>importgc>>>gc.collect()
pyinclr.py
deftestmethod():return"abc"*3
usingSystem;usingPython.Runtime;namespacememoryleakpykw{classProgram{staticvoidMain(string[]args){PyObjectpymodule;PyObjectgc;PyObjecttestmethod;PythonEngine.Initialize();IntPtrpylock=PythonEngine.AcquireLock();{PyObjectsys=PythonEngine.ImportModule("sys");// Py.Import("sys");gc=PythonEngine.ImportModule("gc");// Py.Import("sys");sys.GetAttr("path").InvokeMethod("append",newPyString(@"C:\Users\denis.akhiyarov\source\repos\memoryleakpykw\pyinclr"));pymodule=PythonEngine.ImportModule("pyinclr");testmethod=pymodule.GetAttr("testmethod");}PythonEngine.ReleaseLock(pylock);Console.WriteLine("starting test for memory leak, press any key");Console.ReadKey();pylock=PythonEngine.AcquireLock();{for(inti=1;i<=1000000;i++){testmethod.Invoke();}}gc.InvokeMethod("collect");PythonEngine.ReleaseLock(pylock);GC.Collect();//GC.WaitForPendingFinalizers(); // this blocks executionGC.Collect();Console.WriteLine("finished test for memory leak, press any key");Console.ReadKey();}}}