pythonnet: Memory Leak in python when using a C# DLL

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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Also try building with more verbose mode in setup.py (set VERBOSITY flag)

On Fri, Nov 24, 2017, 10:36 PM Denis Akhiyarov denis.akhiyarov@gmail.com wrote:

Which .NET runtimes and VS versions do you have installed?

Can you try passing the --xplat option to the build?

On Fri, Nov 24, 2017, 4:12 PM BreitA notifications@github.com wrote:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(983,5): warning MSB3644: The reference assembl ies for framework “.NETFramework,Version=v4.0” were not found. To resolve this, install the SDK or Targeting Pack for t his framework version or retarget your application to a version of the framework for which you have the SDK or Targetin g Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

CSC : error CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

got this warning and this error when doing python setup.py bdist_wheel with your branch. Is there any quick fix I can try?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/pythonnet/pythonnet/issues/577#issuecomment-346901335, or mute the thread https://github.com/notifications/unsubscribe-auth/AHgZ5bTScu2DZmAOp3IBGABZoUMgrzP-ks5s5z9jgaJpZM4Qp3jn .