Welcome to MSDN Blogs Sign in | Join | Help

New Article on Detecting Memory Leaks . NET Applications

MSDN has published an excellent article by Fabrice Marguerie entitled “How to Detect and Avoid Memory and Resource Leaks in .NET Applications.” In the article, the author explains how memory leaks are introduced into .NET applications, and what you can do to discover and eliminate them. The code shown in the article is in C#, but the topics covered will likely be useful to a broad range of .NET developers.

 FabriceMarguerie

Figure 1: Fabrice Marguerie is a Microsoft MVP, French Software Architect and Web Entrepreneur with an in-depth understanding of .NET technology.

Fabrice explains his subject in great depth, providing information that will be useful both to advanced developers, and to those with an intermediate-level understanding of .NET development. Features included in the text include:

  • An explanation of how memory and resource leaks occur in .NET application
  • A demonstration of how to detect those leaks
  • A list of common causes of memory leaks and how to avoid them
  • A useful reference section listing a number of tools that can be used to help you find and manage leaks

This is a well written article with a wealth of information in it. Hurry on over to the Dev Center and take a look.

References

kick it on DotNetKicks.com
Published Monday, November 02, 2009 3:49 PM by Charlie Calvert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: New Article on Detecting Memory Leaks . NET Applications

Thanks a lot Charlie!

Please note that the correct address is http://msdn.microsoft.com/en-us/library/ee658248.aspx

Monday, November 02, 2009 5:14 PM by Fabrice

# re: New Article on Detecting Memory Leaks . NET Applications

The garbage collector was touted as being a foolproof mechanism to prevent us poor dumb programmers from creating memory leaks because we forget to free resources we allocate.  It seems the C++ model is much easier - you just always free resources - kind of like you include the closing curly brace.  Now, with C# and the gc, a programmer must know which objects must be disposed and under what circumstances - it has become a convoluted mess.  Microsoft should either elevate the role of the destructor in C# or, better yet, drop the gc altogether.  

Friday, November 06, 2009 5:52 PM by Don

# re: New Article on Detecting Memory Leaks . NET Applications

Don: Drop the gc?? That suggestion shows you really don't know much about the CLR.

Sunday, November 08, 2009 8:34 PM by Chris

# re: New Article on Detecting Memory Leaks . NET Applications

Drop the GC is not an option.

But you are right, that releasing objects in C# it is a real mess. However, this problem is not critical problem like it is in C++ for most cases.

Monday, November 09, 2009 1:05 AM by maze

# re: New Article on Detecting Memory Leaks . NET Applications

Perhaps teach better programming techniques and so programmers don't start developing and releasing software with leaks until they understand the concepts to a good enough standard.

Learn the way of programming without reliance on garbage collection - that was you are responsible for creation and destruction of objects. Learn about memory and how to allocate, use it, free it. Learn about handles and memory associated to not clearing up correctly.

To reiterate - learn to code - not code lazily!

Tuesday, November 10, 2009 9:17 AM by Jay

# re: New Article on Detecting Memory Leaks . NET Applications

My personal opinion is that the root of the problem lies in the fact that most modern-day (upcoming) programmers are spoiled with an abundance of computing resources. My personal experience dictates that junior programmers should really be introduced to programming through the use of limited resource devices like micro-controllers. Programming micro-controllers in ASM whilst using the minimal amount of resources (=make code as efficient as possible), programmers seem to learn to think out of the box when it comes to the use of resources.

Tuesday, November 10, 2009 12:19 PM by George

# re: New Article on Detecting Memory Leaks . NET Applications

My personal opinion is that the root of the problem lies in the fact that most modern-day (upcoming) programmers are spoiled with an abundance of computing resources. My personal experience dictates that junior programmers should really be introduced to programming through the use of limited rere: New Article on Detecting Memory Leaks . NET Applicationssource devices like micro-controllers. Programming micro-controllers in ASM whilst using the minimal amount of resources (=make code as efficient as possible), programmers seem to learn to think out of the box when it comes to the use of resources.

# re: New Article on Detecting Memory Leaks . NET Applications

The most common everyday cause for managed objects being leaked remains the Observer pattern, ie, the event subscription which is never removed.

It's absolutely incredible that the CLR team has not implemented a true weak delegate to fix this problem.  Ian Griffiths tried to fix this issue and messed up the implementation.

At the moment there are no good workarounds to this.  The WeakEvent pattern is klutzy and only works in WPF applications anyway.  The simplest general-purpose solutions use tons of "helper classes" and require lots of code just to hook up and unhook.  And they still depend on some sort of message pumping to free dead listeners on events which aren't firing.

This whole mess stems from the lack of a true Delegate class which holds a weak reference to its target.  The CLR gives us a WeakReference, but no WeakDelegate and no way to create one.

It's unbelievable that this wasn't added to the 4.0 framework -- this problem has been around for years and no one at Microsoft seems to notice or care about it.  Instead we got the System.IObserver<T> which requires lots custom code on the part of the observed, and doesn't mesh with existing event properties at all.

Can anyone from Microsoft comment on this mess?  Is it ever going to be fixed?

Wednesday, November 11, 2009 7:53 AM by Daryl Cantrell

# re: New Article on Detecting Memory Leaks . NET Applications

Daryl, a request has been opened for this in 2004. Microsoft left the request open since then, but there are no signs of any improvement yet...

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94154

Wednesday, November 11, 2009 8:12 AM by Fabrice Marguerie

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker