Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Memory leak when attaching event handlers from Python #1972

Closed
@sean-marten

Description

@sean-marten

Environment

  • Pythonnet version: 3.0.0
  • Python version: 3.7.4
  • Operating System: Windows 10
  • .NET Framework 4.8.4515.0

Details

  • There appears to be a memory leak when attaching a method to an event handler from Python. I was able to break this down into a very simple example. Below is my C# Library
usingSystem;namespaceMemoryLeak{publicClassMemoryLeak: IDisposable{publicevent EventHandler LeakEvent;publicMemoryLeak(){GC.Collect();Console.WriteLine(GC.GetTotalMemory(true));}publicvoidDispose(){this.LeakEvent=null;}}}

(This was the file in my C# class library, just built and took the DLL to use in the below python script)

and the python script that utilizes this library:

importgcimportsysimportclrdefmain():sys.path.append("./dlls")# the dll from the above class library is stored hereclr.AddReference("MemoryLeak")importSystemfromMemoryLeakimportMemoryLeakdefevent_handler():passfor_inrange(200):example=MemoryLeak()example.LeakEvent+=event_handlerexample.LeakEvent-=event_handlerexample.Dispose()delexamplegc.collect()System.GC.Collect()if__name__=="__main__":main()

If you run this python script successfully, you will notice that the total memory steadily increases. If you simply remove the following two lines from the python script regarding the addition of the event handler into the C# object, you will notice no memory increase:

example.LeakEvent+=event_handlerexample.LeakEvent-=event_handler

I would expect that between the cleanup of the attached handler, disposal and manual collections from both python and C#, that the event handler + memory leak class would get garbage collected properly, but they do not appear to be removed entirely based on memory tracking.

To note, I did attempt to make a C# console application that performs the same steps as the python script and the memory did not leak in that example.

I am more than happy to help contribute if this is deemed to be an issue with pythonnet, but would need a little guidance on where to start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp