My application creates and destroys a number of .NET windows forms. Each form is closed and disposed properly, but are never garbage collected. From analysis with tools like dotMemory, it seems that the forms are being treated as GC roots. Is there a way to prevent this or force the garbage collector to clean up the form and its referenced objects?
Asked
Active
Viewed 104 times
0
-
Related: [There are four main kinds of root in .NET](https://stackoverflow.com/a/8458890/4934172). – 41686d6564 stands w. Palestine Jun 22 '18 at 22:43
-
Also: [How do I make sure a winform is garbage collected?](https://stackoverflow.com/q/17637114/4934172) – 41686d6564 stands w. Palestine Jun 22 '18 at 22:48
1 Answers
1
It won't be acting as a GC root.
Your problem might be related to events i.e. you'll have an event source that is outliving one of your event listener objects. When you subscribe to some event external to the form, that event source will keep a reference back. This will prevent your form from being garbage collected until the event source object is garbage collected. One possible culprit is a static object as an event source?
Have another go in the memory profiler with this in mind.
If it is the problem you should be able to fix by unsubscribing on a form closed event.

Ben Hall
- 1,353
- 10
- 19