basquang™ on clouds

December 8, 2011

How to check for memory leaks in WP7 pages

Filed under: Windows Phone — basquang @ 4:10 PM

In your App.xaml.cs, in your CompleteInitializePhoneApplication method add:

System.GC.Collect();
System.GC.WaitForPendingFinalizers();

Add this code to each page and if you do not recieve a messagebox when going back then you may have a memory leak. Sometimes you have to navigate to other pages to recieve a messagebox.

~MyPage()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(new System.Action(() =>
{
System.Windows.MessageBox.Show("MyPage");
}));
}

Known Memory Leaks
1. I discovered that one of my leaks is definately due to a problem with the ContextMenu in the Silverlight Toolkit. A patch can be found here. I have not had time to confirm that the patch works.
http://silverlight.codeplex.com/workitem/7089
2. The Bing Maps Silverlight control has a memory leak which is caused by pressing the back button while the it is still loading. I can confirm that this is the case on my phone but need to check with the new update on the emulator. See details here:
http://connect.microsoft.com/VisualStudio/feedback/details/625627/wp7-bing-map-control-memory-leak
The workaround in the thread (shown below), cancels the back key press on some occasions which is less than ideal. Again I have not had time to try it yet but I think a better workaround may be to remove the map from the page altogether.

protected override void OnBackKeyPress(CancelEventArgs e) 
{ 
this.map.SetMode(new NullMode(), false); 
if (this.map.IsDownloading || !this.map.IsIdle) 
{ 
e.Cancel = true; 
} 
} 

Unknow Memory Leaks
I found that other pages had a leak which contained no third party controls, code behind or custom attached properties. The only thing this page contains is xaml and I am setting the DataContext of the page to my view model.
I discovered a work around, which is to set the DataContext to null when navigating away from the page. I spent a little time trying to recreate this problem in a sample project but without success so far. My next step is to copy the xaml from this page into my sample project and go from there. I will upload a sample project when I find an answer.

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
this.DataContext = null;
}

Source

Advertisements

1 Comment »

  1. Hi! Are you using MVVM Light Toolkit? If so the problem perhaps is the RelayCommand. It creates a strong reference which keeps the Datacontext and hence the page in memory.

    Comment by Mike — October 3, 2012 @ 10:39 AM | Reply


RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: