-
Notifications
You must be signed in to change notification settings - Fork 747
Memory leak when attaching event handlers from Python #1972
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
Comments
Output from full python script (in bytes):
Output when event handler lines are removed (in bytes):
If you run even more cycles, the problem will exacerbate as well. |
I don't see you calling .NET GC anywhere. |
Do you mean I am not manually calling collect? |
@lostmsu Sorry I was typing over from a different computer, I was actually manually calling EDIT: I saw a different issue where you also mentioned someone was not calling .NET GC, and by those comments I assume that you mean that I am not doing some sort of: import System
# run some code...
System.GC.Collect() ...in my python script. I have tried this out and edited my original python script and the results are still the same. |
@sean-marten please, doublecheck the output with all the GC calls, then if it reproduces, I can take a look at it. |
Okay @lostmsu , so I re-ran with the .NET GC collection calls both at the end of the python loop and in the dispose method of the C# library. Also for posterity I still am running the python gc collection at the end of the python loop. I also added a dummy array object to the import gc
import sys
import clr
def main():
sys.path.append("./dlls") # the dll from the above class library is stored here
clr.AddReference("MemoryLeak")
import System
from MemoryLeak import MemoryLeak
def event_handler():
pass
for _ in range(200):
example = MemoryLeak()
example.LeakEvent += event_handler
example.LeakEvent -= event_handler
example.Dispose()
del example
gc.collect()
System.GC.Collect()
if __name__ == "__main__":
main() using System;
using System.Linq;
namespace MemoryLeak
{
public Class MemoryLeak : IDisposable
{
public event EventHandler LeakEvent;
private Array arr; // dummy array to exacerbate memory leak
public MemoryLeak()
{
this.arr = Enumerable.Range(0, 10000).ToArray();
}
public void Dispose()
{
this.LeakEvent = null;
GC.Collect();
Console.WriteLine(GC.GetTotalMemory(true));
}
}
} When run normally, we see the min and max of the If you remove the python lines regarding registering the event handler from python, the output of Please let me know if you would like me to do any further diagnosis! |
…re event handlers fixes pythonnet#1972
…re event handlers fixes pythonnet#1972
…re event handlers fixes pythonnet#1972
Environment
Details
(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:
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:
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.
The text was updated successfully, but these errors were encountered: