Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.
Check out JobNimbus - CRM for Contractors and Service Professionals.
Silverlight Memory Leak DataGrid, DataForm, DataTemplate, etc...
Silverlight 4 is a fantastic platform. Unfortunately it suffers from memory leaks on some of the standard controls if they are created in a certain way and some third party controls will leak memory.
On the Silverlight forums there is a thread that can be found here: http://forums.silverlight.net/forums/p/171739/466567.aspx#466567 For example, if the XAML is created like this:
<Grid x:Name="LayoutRoot" Background="White">
<controls:DataGrid AutoGenerateColumns="False">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="TextBlock"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellEditingTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
</Grid>
The grid object will never be released in garbage collection due to a bug in the Silverlight engine.
As a work around to this bug, Tim Heuer and his team at Microsoft suggest that the DataTemplate be changed to a Static Resource like this.
<UserControl.Resources>
<DataTemplate x:Name="testTemplate">
<TextBlock Text="TextBlock"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CellEditingTemplate="{StaticResource testTemplate}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
This grid object will be released in garbage collection.
In order to quickly identify a whether a control leaks, I built a Visual Studio 2010 solution which dynamically creates control(s) and adds them to a StackPanel and a WeakReference list. Then when the user clicks Remove, all controls are removed from the StackPanel and a garbage collection is done. The WeakReference list is checked and any controls still “Alive” in the WeakReference list can be considered memory leaks.
Here are the steps to follow:
private void ButtonAddItem_Click(object sender, RoutedEventArgs e)
{
AddItem(new TextBox());
AddItem(new StackPanel());
AddItem(new ComboBox());
AddItem(new SilverlightUIElementLeakTester.UserControls.DataGridInlineTemplate());
AddItem(new SilverlightUIElementLeakTester.UserControls.DataGridStaticTemplate());
// TODO - Add other AddItem calls here to see if UIElement derived controls leak.
}
* Add any additional UIElement derived controls here that you would like to test for memory leaks.
*** Possible memory leaks in the objects below or their children. *** *** Clear memory again and see if any of the objects free from memory. *** SilverlightUIElementLeakTester.UserControls.DataGridInlineTemplate ---- GC.GetTotalMemory(true): 362636 Bytes, 0 MBAll objects were released from memory except for the UserControl that has a data grid with an inline datatemplate. Note that I have found this solution invaluable for identifying other controls that are leaking in Sivlerlight. I used this solution to figure out that the DevExpress RichEdit box leaks and is never released. I will be opening a support ticket with them shortly. Thanks to Davy Bryon’s ObjectTracker class which is included in the solution. His article can be found here: http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in... Thanks to http://manoli.net/csharpformat/ for the code formatting.
Popular Articles
Last viewed:
- Change Password Policy on Windows 2003 Server
- Microsoft Word Spell Checker Doesn't Check Cut and Pasted Text
- Launch URL in Default Browser using C#
- ASP.NET 301 Redirect for Search Engine Compliant Redirects
- Data Access Layer using SqlDataReader and C#
- Export GridView or Other Data as Microsoft Excel (CSV) File with C#
Recent comments
- maybe this will help you alot
8 hours 23 min ago - Reply to comment - Code Samples & Tutorials
1 day 4 hours ago - thank you for sharing
3 days 1 min ago - Great explanation and more questions
4 days 3 hours ago - Insertion of illegal Element:
4 weeks 6 days ago - Insertion of illegal Element: 32
4 weeks 6 days ago - re "But, this will NOT work."
6 weeks 11 hours ago - Unable to cast COM object of t
6 weeks 14 hours ago - Saved my life
6 weeks 1 day ago - nice
9 weeks 11 hours ago

Can't download
Hello,
I can't download that file. It's forbidden!