Thursday, July 7, 2011

Memory Leaks in Silverlight Toolkit

Hi all.
In this post I'll try to share my previous experience about memory leaks I've faced in one of projects I've working on.
The problem was in DateTimPicker control of the Silverlight Toolkit, and also in Accordion. Seems those were not being garbage collected and causing all the stuff related to them even with event handlers to stay there in memory.
So here are few simple tips to follow to avoid such things: Always be sure that you've unregistered any event handlers from the above mentioned controls, because those will make the objects, which in those handlers defined to also stay in memory.
If the controls are defined in a XAML file, just remember to remove it from its parent container. All this is easily handled by a simple interface implemented on a control level (you can call it IReleasable for example), and just call the Release method (defined in this interface) from the Page's unload point. And in this method do this "detach" from the leaking control. Why the unload is not a proper place- because for a control it may fire many times - as the Load event.

Good luck with Silverlight development.

No comments: