- Notifications
You must be signed in to change notification settings - Fork750
Open
Description
Environment
- Pythonnet version: 3.0.1
- Python version: 3.9.12
- Operating System: Windows or Linux
- .NET Runtime: net6.0
Details
- Describe what you were trying to get done.
We are embedding Python in a .NET app using pythonnet.
The call toPythonEngine.Shutdown()
at the exit of this app lasts formore than 2 minutes.
Looking at the shutdown with a profiler shows that most of the time is spent inRuntime.TryCollectingGarbage()
.
Looks like the more memory is allocated on the C# side the more the shutdown (i.e. calls toGC.Collect()
) is slow.
- Minimal, Complete, and Verifiable example
usingPython.Runtime;namespaceTest;staticclassProgram{staticvoidMain(){varmem=newList<Array>();Runtime.PythonDLL=@$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\Local\Programs\Python\Python39\python39.dll";PythonEngine.Initialize();varthreadState=PythonEngine.BeginAllowThreads();using(Py.GIL()){using(varscope=Py.CreateScope()){scope.Set("a",1);scope.Set("b",2);scope.Exec("result = a + b");varresult=scope.Get("result").As<int>();Console.WriteLine($"Sum ={result}");}}// allocate dotnet-managed memory unrelated to pythonnetvarrnd=newRandom();for(vari=0;i<100_000_000;++i){mem.Add(newbyte[rnd.Next(1,10)]);}PythonEngine.EndAllowThreads(threadState);Console.WriteLine("Shutdown start");varstart=DateTime.Now;PythonEngine.Shutdown();vardiff=DateTime.Now-start;Console.WriteLine($"Shutdown stop{diff.TotalSeconds}");}}
Typical output:
Sum = 3Shutdown startShutdown stop 61.6796983
Metadata
Metadata
Assignees
Labels
No labels