Skip to content

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

Closed

Description

@RomzMcGriess

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();
    }

Activity

  1. filmor commented on Jul 6, 2017

    @filmor
    Member

    Can you check this against master?

  2. dmitriyse commented on Sep 6, 2017

    @dmitriyse
    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions