I want to know if really memory is getting leaked. My Scenaio is like:
Is this a memory leak or could be a situation that garbage collector have not cleared the memory.
And as events are also considered as references. what if the event is present in a dereferenced object? then that event would not be considered as a reference, right?
The garbage collector frees only objects that aren't referenced anymore.
It doesn't magically remove all the objects you don't want anymore.
Check if you still have references to any of the objects. Remember that events are also considered references. (It needs to know which object to go to)
Memory is only garbage collected when needed, more technical when the so called Generation 0 is full or when the overall system memory pressure is suficcient to force a garbage collection. Memory is not automatically reclaimed when it goes out of scope. More info here and here.
Just for testing, try calling GC.Collect()
after closing the dialog (after it is no longer referenced) to force the GC to collect any available memory.
In reality, you should not fiddle with the Garbage Collector, it is heavily tuned for best performance.
If you suspect that you are indeed leaking memory, use some kind of memory profiler to analyze your application.
Try for example RedGates Memory Profiler, they have a time-based trial.
Follow this walk-through to get up to speed and learn a bit what to look for and how.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With