Skip to content

Memory leak when PythonEngine.Initialize/Shutdown is called in a loop #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RomzMcGriess opened this issue Jun 19, 2017 · 2 comments
Closed
Labels

Comments

@RomzMcGriess
Copy link

RomzMcGriess commented Jun 19, 2017

Environment

  • Pythonnet version: 2.3.0
  • Python version: 3.6.0
  • Operating System: Win7 x64

Details

Hello all together,
i started to use pythonnet for my application. I discovered a memory leak. I reduced my code to this to identify the leak. It appears that Python.Shutdown() does not clear all references and memory. Here is a picture of my memory diagnostics. Do i miss something? Maybe someone of you has some info.

Memory diagnostics picture

Here is my c# code:

    static void Main(string[] args)
    {
        for (int i = 0; i < 10000; i++)
        {
            Console.WriteLine($"Cycle: {i}");

            Console.WriteLine($"Status: {PythonEngine.IsInitialized}");

            if (!PythonEngine.IsInitialized)
            {
                Console.WriteLine("Initialize");
                PythonEngine.Initialize();
            }

            Console.WriteLine($"Status: {PythonEngine.IsInitialized}");

            if (PythonEngine.IsInitialized)
            {
                Console.WriteLine("Shutdown");
                PythonEngine.Shutdown();
            }

            Console.WriteLine($"Status: {PythonEngine.IsInitialized}");
        }

        Console.ReadLine();
    }
@filmor
Copy link
Member

filmor commented Jul 6, 2017

Can you check this against master?

@dmitriyse
Copy link
Contributor

Flat Py_Initialize/ Py_Finalize call cylce will produce memory leak, it's directly described in the Python C Api documentation.
For example each run of all Python.EmbeddingTest, eats 5mb of memory. It's more python itself. Nothing to do with this.

If you have larger memory leak, please try this branch #532
This PR reduces memory leaks of a working Python.Engine.

@den-run-ai den-run-ai added the bug label Apr 18, 2018
@filmor filmor closed this as completed Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants