Sunday, September 2, 2012

Memory leak in silverlight prism/unity

Memory leak in silverlight prism/unity

We are building a LOB application using SL4, PRISM and MVVM. I noticed very lately that my App is not releasing any memory. It starts around 150MB and grows to 1 GB+ very fast if I switch b/w pages. Our design involves 3 modules each having a Controller class (Unity singleton) which listens for events and injects views into the appropriate region. So this is whats happening in those controllers when an event is fired to inject a view.

var v = Container.resolve<view>() var vm = Container.resolve<viewmodel>() v.datacontext = vm; myregion.addtoregion(v); 

so 100 calls for this view will exceute this 100 times. And in addviewregion method, I'm removing all existing views before adding the new one. And after seeing the memory problem, I set both view & viewmodel to null in addviewtoregion method for all the views needed to be removed. Still memory consumption is same. Is Unity ContainerControllled... objects (my controllers) holding references to these objects?

Answers & Comments...

Answer: 1

Are you registering to handle events on these dynamic views and forgetting to unregister them before you remove the view? Handling an event creates an implicit reference to the object, and that is typically where managed memory leaks come from.

You should check out this guide to tracking down leaky Silverlight controls using WinDbg to examine the heap and GC roots.

by : Dan Auclairhttp://stackoverflow.com/users/58132




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog