WPF & Memory Leaks
The Red Herring:
Recently, I had to track down why all of our new WPF screens were leaking memory like a sieve. A coworker of mine ran into a discussion on the WPF forum and my jaw dropped. I rapidly dove into this issue and soon discovered that this issue was not really the one causing us problems (that I could tell). It seems as if this issue only raises its ugly head in certain situations (see the post for more detail).
The Awesome Tool:
At this point, we ran into a tool that, in my opinion, is a must have if you are trying to find memory leaks in a .NET application. It is called (aptly enough), .NET Memory Profiler and it is made by a company called SciTech software. Previously, we had been using AQtime and I still would recommend that tool for performance profiling but when it comes to memory allocation profiling (i.e. memory leaks), it is quite slow. The .NET Memory Profiler doesn’t do performance profiling and focuses exclusively on memory allocation profiling. I think this has made it the best tool out there. I have tried Jet Brains dotTRACE as well, which is also very fast, but the .NET Memory Profiler beats that one hands down in how it presents the information to the user and how it allows you to navigate through all the objects that have been created or are still living on the heap. They do offer a free 14-day trial and I would recommend that you try their 3.5 version which you can preview now. This version has a very nice feature that they call Automatic Memory Analysis, which is basically tips and warnings regarding common memory usage issues.
The Real Issue:
Using this tool and some grit, the previously mentioned coworker and I finally ran into the issue that was causing us problems. It is a known issue. Personally, I love what it says at the knowledge base article just referenced:
When you use data binding in Microsoft Windows Presentation Foundation (WPF), a memory leak may occur.
Ha! Are you kidding me?!
You can check out the knowledge base article for more info, but basically, if you are binding to something that isn’t a DependencyProperty or doesn’t implement INotifyPropertyChanged, WPF tries to use the ValueChanged event. In doing so, it calls PropertyDescriptor.AddValueChanged which creates a strong reference from the property descriptor to the object being bound to.
Something as simple as the following xaml causes a leak:
<Label Name="MyLabel">
<Stack Panel Name="MyStackPanel">
<TextBlock Text="{Binding ElementName=MyStackPanel, Path=Children.Count}" />
</StackPanel>
</Label>
One way that you can verify that this issue is causing you problems is to see PropertyDescriptor in the root path. Check out this screen capture:

The Solution:
So, how did we rid ourselves of this problem? Well, we basically went in and modified the objects that were causing us leaks to inherit from INotifyPropertyChanged … even if we didn’t really want to notify anyone of changes. In the those cases, we would #pragma the CS00067 warning away that came from not using the PropertyChanged event:
public class SomeClassBeingDataBoundTo : INotifyPropertyChanged
{
// class implementation
#pragma warning disable 0067
// If you data-bind to a regular .NET property on a regular .NET object, that object will never get garbage-collected.
// • Dependency properties are not affected.
// • Objects implementing the INotifyPropertyChanged interface are not affected.
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore 0067
}
How the heck did I miss this?
So, after all the excitement subsided, I did a Google search to see if I could find any other blogs that talks about this problem. Heh heh. The link at the top of search results … was about our issue. Argh! I don’t know how I missed it before when I was doing my original research.
Even better, the search results brought up Jossef Goldberg’s priceless post on finding memory leaks in WPF applications (that I’ve run into before). That post is a must read if you are trying to find leaks in your WPF applications. And, yes, it too, talks about our issue.
Someone, please take my software engineering card away.
I hope my pain is someone else’s gain.
