Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

WPF memory leak issues & fixes.

I hope some of you might’ve already gone through the recent Redgate article on memory leak.

RICKY LEEKS PRESENTSTHE TOP 5 – WPF AND SILVERLIGHT GOTCHAS-Pdf (by Redgate)

Src : http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/learning-memory-management/resources/RickyLeeks-MisconceptionsLong.pdf

Key take away,

1)   UnRegister events  – .net funda

2)  DataBinding issue – <Grid Name=”mainGrid”>

<TextBlock Name=”txtMainText” Text=”{Binding ElementName=mainGrid, Path=Children.Count}” />

</Grid>

Issue:-  The condition will only occur if the bound property is a PropertyDescriptor property, as Children.Count is. This is because, in order to detect when a PropertyDescriptor property

changes, the framework has to subscribe to the ValueChanged event, which in turn sets up a strong reference chain.

Description :

-If the binding is marked as OneTime, the bound property is a DependencyProperty, or the object implements INotifyPropertyChanged, then the issue won’t occur.

-In the case of OneTime binding this is because, as the name suggests, it doesn’t need to detect property changes, with the binding occurring once from data source to consumer.

Solution: (many ways)

–          Add a DependencyProperty to the page/window which simply returns the value of the required PropertyDescriptor property  (or )   Make the binding OneTime – Text=”{Binding Path=Salary, Mode=OneTime}”/    (or)   Add the following line of code on exit from the page:

BindingOperations.ClearBinding(txtMainText, TextBlock.TextProperty); This simply clears the binding and removes the reference.

3)      Static Events – Statics, once referenced, remain for the duration of the app domain execution

4)      Command Binding – Child window command binding references in Main window need to be explicitly removed on close of Child windows.

Ex: mainWindow.main.CommandBindings.Remove(cutCmdBinding);

5)      Dispatcher Leak – .net funda

6)      Textbox Undo leak – new

Issue :- Problem is to do with the TextBox control and UNDO. TextBoxes have built-in undo functionality, enabling a user to undo their changes to a text box

Fix:- You can limit the behavior of the undo stack by either switching it off using: textBox1.IsUndoEnabled=false;

This limits the number of actions that can be undone, in this case to 100; By default the setting is -1 which limits the number of actions only by the amount of memory available. Setting the value to zero switches undo off. –

<Textbox UndoLimit=-1 Width=100 Height=20 …./>

Note: For couple of weeks, I tried redgate for WPF profiling – Honestly I m not able to understand or make out what exactly steeling my memory with regards to WPF. It points to a WPF class and goes on and goes on as a chain. As a developer, I feel difficult to understand the redgate’s chart diagram ( was expecting it will point me /refer to a class where the leak has happened.. but full of WPF classes ). any comment?

Memory leak by WPF Resources : http://weblogs.asp.net/lkempe/archive/2009/04/17/memory-leak-with-wpf-resources.aspx

Memory leak by WPF Animation: http://blog.ramondeklein.nl/index.php/2009/02/20/memory-leak-with-wpf-animations/

Advertisements

August 22, 2011 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: