0

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?

nloewen
  • 1,279
  • 11
  • 18

1 Answers1

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