Skip to content

Memory Leak in python when using a C# DLL #577

@BreitA

Description

@BreitA

Environment

  • Pythonnet version: 2.3.0
  • Python version: 3.6.1
  • Operating System: Windows 7 Professional 64bits.
  • .NET Framework 4.6

Details

  • I experience memory leaks when trying to use a C# dll in python to run a simulation and retrieve results using python. When I try to run multiple time the calculation I get leaks
    Here is a template of code I run to experience this memory leak (replaced the name of the dll I use by generic names)
import clr
import os
import sys
import psutil
import gc
dll_path = r''the path to my dll"
sys.path.append(os.path.join(dll_path, 'MyDll.dll'))
clr.AddReference(os.path.join(dll_path, "MyDll"))
while psutil.virtual_memory().percent < 80:
    print('psutil_virtual_memory:', psutil.virtual_memory().percent)
    obj = MyDll.MyObject()
    res = obj.Run() # long simulation is run and I get the results.
    gc.collect()
    clr.System.GC.Collect()

Output :
psutil_virtual_memory: 41.8
psutil_virtual_memory: 44.4
psutil_virtual_memory: 46.6
psutil_virtual_memory: 48.9
.....
until my memory reach 80%

The issue is that running the equivalent code in C# does not give any memory leak but it's when calling it from python we get issues of memory leaks. the python garbage collector doesn't help at all with this. I tried also to call the System GC Collect but without success too.

My question is how can I investigate and try to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions