Monday, February 08, 2010

How to detect and avoid memory and resources leaks in .NET applications

Despite what a lot of people believe, it's easy to introduce memory and resources leaks in .NET applications. The Garbage Collector, or GC for close friends, is not a magician who would completely relieve you from taking care of your memory and resources consumption.

I'll explain in this article why memory leaks exist in .NET and how to avoid them. Don't worry, I won't focus here on the inner workings of the garbage collector and other advanced characteristics of memory and resources management in .NET.

It's important to understand leaks and how to avoid them, especially since they are not the kind of things that is easy to detect automatically. Unit tests won't help here. And when your application crashes in production, you'll be in a rush looking for solutions. So, relax and take the time to learn more about this subject before it's too late.
Table of Content

   * Introduction
   * Leaks? Resources? What do you mean?
   * How to detect leaks and find the leaking resources
   * Common memory leak causes
   * Common memory leaks causes demonstrated
   * How to avoid leaks
   * Tools
   * Conclusion
   * Resources

Introduction

Recently, I've been working on a big .NET project (let's name it project X) for which one of my duties was to track memory and resources leaks. I was mostly looking after leaks related to the GUI, more precisely in a Windows Forms application based on the Composite UI Application Block (CAB).
While some of the information that I'll expose here applies directly to Windows Forms, most of the points will equally apply to any kind of .NET applications (WPF, Silverlight, ASP.NET, Windows service, console application, etc.)


Read more: MSDN

Posted via email from jasper22's posterous