Silverlight 4 seems to be memory hungry
I have been playing around with Silverlight a bit and am trying to port my Silverlight 3.0 application to Silverlight 4.0. My application downloads different XAP files and upon user request instantiates a custom Xaml control and adds it to the main container as a MEF approach so that I can have an extensible and pluggable application.
The application is quite cumbersome, and in order to maintain acceptable performance and bootstrapping, I created some helper classes to load all pages and custom controls in the background that can be used later.
In Silverlight 3.0, everything went smoothly without any issues. Moving to SL 4.0 I noticed that when the process gets closer to instantiating custom controls, the layout will suddenly freeze for a minute or more. Looking at the Task Manager, IE memory usage jumps from 50MB to 400MB, and sometimes to 1.5GB.
If the process doesn't take up a lot of space, the layout will display correctly, although the memory usage is still extremely high. Otherwise, all crashes are due to a memory exception.
Running the same application compiled in SL3, the memory used is about 200MB when all custom controls are loaded. The time it takes to load an application in SL3 is about 10 seconds, while SL4 takes up to 3 minutes. The layout has no transparencies, no transparency, no effects or animations.
Custom controls run on the fly and are added or removed in the visual tree when the user switches from one screen to another. All resources are cleaned up properly when the custom control is removed from the render tree to allow the GC to run in the background.
I may be doing something wrong, but I could not figure out exactly where the nail of the source of this problem is. As far as I know, there is no memory profiler in SL4 that can help me find what to look at. But again, I couldn't be updated with the new debugging tools.
a source to share
UPDATE:
Silverlight 4 Service Release to fix memory leaks: http://timheuer.com/blog/archive/2010/09/01/silverlight-service-release-september-2010-gdr1.aspx
Silverlight 4 has known memory leaks and a fix is currently being tested.
Here's about this Microsoft Thread:
User "heuertk" is a Microsoft Silverlight developer ... he explains the issues and the status of the fix ...
a source to share
To be honest, despite your statement, "All resources are cleaned up properly when the user control is removed from the visual tree" is where I start looking. In fact, the scent of UserControls is not properly disposed of.
This usually happens when there are long-lived (e.g. static) objects that trigger events to which more transient objects are attached. If these event handlers are not detached, these objects remain in memory.
a source to share
I continue testing and want to share what I have found so far. It might be helpful to understand the behavior of SL4.
As it sounded rather odd, UIThread can take so long to render a bunch of graphical components and given that Microsoft has improved the rendering pipeline, I reverted my solution back to SL3, but I kept SL4 on my localhost.
The application uses RIA services and moving back and forth to SL4 means that I have to make some code changes according to the released documentation.
The application runs very smoothly more than it did when tested with Visual Studio 2008. Memory usage is lower than it was before when SL4 was not installed.
As soon as I switch to VS2010 it's a completely different scenario. The memory grows indefinitely, the layout is slow to catch user interaction, and sometimes it freezes, as explained earlier.
I disabled RIA services using the standard Rest service and the process hasn't changed in quality.
In conclusion, given that I will continue to test, I can finally understand what is really stopping me from getting the application in an acceptable mode, I believe that the memory issue is related to the VS 2010 debugging process or a combination of VS 2010 and SL4
a source to share