WPF memory leaks

After working on a WPF application for a couple of months, I’ve noticed that the application was leaking a large chunk of memory. After some memory profiling, the culprit was not very apparent. It seems that it was a combination of issues that contributed to the leak. One of the biggest problem was that data-binding was not done “properly”.

So what do I mean by “data-binding not done properly? Apparently if your DataContext does not implement INotifyPropertyChanged, you’re in big trouble!! Here’s a link from Microsoft that explains the phenomenon….

After much research, I came across a fantastic article explaining all the possible errors one can make to cause a memory leak. Some of these are not WPF inherent problems, they are just typical memory leak culprits dominant in all .NET applications, e.g. failure to unsubscribe an event handler; while some are just obscure and weird. But rest assured that some of the weird ones are fixed in .NET Framework 3.5 SP1 and .NET Framework 4…. So if you develop in WPF, this article is a MUST READ!!!!!

Lastly, a word of caution. Remember to start performance testing and memory profiling quite early in the stages of development. It’s a misconception that these activities can be put off until the late stages. If you do that, you have to bear the consequence of not being able to fix these problems because it becomes too hard to change code and change design…you have been warned.

Share this post :