简体   繁体   中英

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?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM