Wednesday, January 10, 2007
Everyone who knows me knows that I have a special interest for memory leaks in .Net. At the company where I work we have solved a lot of these issues and I’ve also had talks about this at Norwegian .Net User Group (NNUG) in Bergen and Stavanger. One of our biggest problems was memory leaks related to events. Rico Mariani (MS performance guru) has set focus on this in his blog today by this blog post, so go check it out!

He focuses on the issue related to event generators, but there are other areas where you can get into trouble as well. I’ll try to explain our scenario:
  1. We have a graphical engine responsible for creating graphical components (user controls) dynamically by using reflection.
  2. All of these user controls inherit from the same class which exposes a lot of events.
  3. When our graphical engine creates new instances of these user controls it hooks up these events.
This model is created to have loose coupling between the user controls, the engine and other components we have in our app. The engine has a lot of knowledge about other components and services in our app, which sometimes our user controls wants to communicate with. This communication is done through these events. Here’s an example:
I have a user control that wants to tell a service that he have made some changes the service should know about. For this we have created a special event on the user control that the engine listens to. So I trigger this event on the user control, the engine receives it and tells the service about it.
com.png

These event hookups caused us a lot of pain at some point. Our solution was the first that Rico mentioned in his blog post (IDisposable), which I personally would prefer. In addition to this we added some reflection code which found events and removed them. You’ll find more info about this here: http://channel9vip.orcsweb.com/ShowPost.aspx?PostID=180985

Before I end I want to recommend two tools you can use to find memory leaks in .net. The first one which we use is called .Net Memory Profiler. The other one which I’ve not personally used but I’ve heard others are using is the ANTS Profiler. So go find your leaks. Happy hunting!
.Net | MemoryLeaks | Microsoft | NNUG | Work
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview
 
Aggregate Me!
Feed your aggregator (RSS 2.0)  Rss
  Comments
On this page....
Locations of visitors to this page