I have a Silverlight 4 application which has a leaky ViewModel class. I have confirmed this by using WinDbg and the !gcroot command on my ViewModel type. I was unable to pin point the exact cause of the memory leak but have attempted to resolve it by using the following line of code in the corresponding view when it is closed:
HtmlPage.Window.Navigate(new Uri(MyTopLevelUri));
This forces the app to restart and hence the RootVisual is reinitialised. Now when I use WinDbg I can see that there are no more references holding onto the ViewModel class after the View has closed.
However, when I monitor the memory use of the app (using SysInternals) it continuously increases. Sometimes it may drop slightly but if I keep opening and closing the suspect View I eventually end up with a private byte memory set of over 1,000,000K. Also, even if I add code to force the GC, it never truly recovers all the memory allocated. Should I be worried?
Are there inherent memory leaks with SL4 and the controls? I don't have much faith given the patch that was released recently to fix memory leaks with DataTemplates (version 4.0.60129.0 http://timheuer.com/blog/).
It appears that the answer to my original question (are there inherent memory leaks with SL4?) is indeed yes!
http://forums.silverlight.net/forums/t/211504.aspx
https://connect.microsoft.com/VisualStudio/feedback/details/649246/usercontrol-resources-memory-leak-silverlight-4-0-60129-0
The two leaks described in the links above have apparently been fixed in SL5.