Monday 23 August 2010

Silverlight Animation Memory Leak in FillBehaviour

My first post of significance! Yay!

After months, *litrally* months of debugging, heap walking and cursing, with the help of the kind people at Microsoft and the Silverlight team, I’ve found the root cause of my memory leak.

The situation was, that I was using an ItemsControl with DataTemplate, whose contents animated independently.

To create the animation, I didn’t declare the fill behaviour of the storyboard, and consequently it defaulted to HoldEnd, which of course kept the last value of the storyboard. All well and good, and this was the desired behaviour.

Turns out though, that the animation I was using, is only properly cleared up from using two of the following:

  • Using FillBehaviour=”Stop” in xaml
  • Using Storyboard.Stop() in code

Great! So hold end doesn’t clean up resources.

The solution is to drop the hold end usage and somehow work in Stop somewhere. After that your memory leak should be significantly less!

Again, not sure about the mechanics as its a bug in the Silverlight libraries, but from my testing, switching fill behaviours certainly works! :)

I’ll try and embellish this post soon with more information, but I’m currently still trying to understand the full implications myself!

Hope this helps someone!