Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in .Net

I want to know if really memory is getting leaked. My Scenaio is like:

  1. My .Net application is taking x amount of memory.
  2. i opened some dialogs and now it takes x+y amount of memory
  3. Closed all the recently opened dialogs
  4. still memory is around x + Y

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?

like image 497
Make It Perfect Avatar asked May 24 '11 12:05

Make It Perfect


2 Answers

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)

like image 130
Yochai Timmer Avatar answered Sep 27 '22 22:09

Yochai Timmer


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.

like image 34
Jakob Möllås Avatar answered Sep 27 '22 22:09

Jakob Möllås