5

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?

Make It Perfect
  • 935
  • 2
  • 10
  • 20
  • 1
    Your question is vague. Would you provide more information? Post some code or give us a real-world scenario leading to memory leak. – Alireza Maddah May 24 '11 at 12:17
  • Just want to know that: As Memory is not getting freed even after closing the opened dialogs, does that means its a memory leak or could it be a case of memory still not garbage collected/ – Make It Perfect May 24 '11 at 12:18
  • 4
    Allocating memory is expensive. .NET apps allocate memory, but don't return it to the system unless necessary. Stop looking at Task Manager; it doesn't tell you anything. You'll know you have a leak/memory issues when you get an OOME. –  May 24 '11 at 12:21
  • 1
    Additionally to Wills comment you should also try to open/close the dialogs several times to see if the consumption really climbs up and up again. To be really sure you need a memory profiler like [ANTS](http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/) from redgate. – Oliver May 24 '11 at 12:27

5 Answers5

10

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)

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • 2
    Several times I've been caught out by events holding onto objects. +1 – Matt Ellen May 24 '11 at 12:20
  • Absolutely right. But as per my knowledge garbage collector does not collect dereferenced objects immediately. It collects objects only if application is getting low on memory. So it could be possible in my scenario that all objects are dereferenced but still not cleared from memory. Right? – Make It Perfect May 24 '11 at 12:21
  • It's a bit more complicated than that. There are more things it takes into consideration. Think about it, a 10 Mb program won't wait to fill up 4gb before it starts freeing. It won't just wait for the memory to run out. – Yochai Timmer May 24 '11 at 12:25
  • Make a simple test button that creates the objects, and then runs out of scope. click a few times. See if memory keeps increasing (it shouldn't) – Yochai Timmer May 24 '11 at 12:26
  • one more qn: As events are also considered as references. what if event is present in a dereferenced object? then that event would not be considered as a reference, right? – Make It Perfect May 24 '11 at 13:06
  • 1
    Ok found that memory leak was happening thru some of events and even some objects were leaking. Thanks all – Make It Perfect May 26 '11 at 07:46
2

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.

Community
  • 1
  • 1
Jakob Möllås
  • 4,239
  • 3
  • 33
  • 61
1

In .Net once you dont reference an object the object is collected back. If you have a live reference then the object is kept alive. So to find a memory leak you need to do the following

  1. Find the list of objects and the memory being occupied
  2. Identify the objects that you doubt should have been freed by now.
  3. Find the root that is holding this object.
  4. Fix the root.

You can either do this with some memory profiler or you can use WinDbg+sos. People feel windbg is hard to use but for these cases windbg+sos is much more easier to use and it is free. Also you will feel happy after fixing the leak. you will be one among those who always prefers free powerful tools.

Have a look at this link.

http://blogs.msdn.com/b/ricom/archive/2004/12/10/279612.aspx

http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-memory-leak.aspx

ferosekhanj
  • 1,086
  • 6
  • 11
0

How about using a memory profiling tool such as JetBrains dotTrace to profile the memory usage of your application?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
-1

It could be a situation that garbage collector have not cleared the memory

Umesh CHILAKA
  • 1,466
  • 14
  • 25