How to check for memory leaks in WP7 pages

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


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.

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(new System.Action(() =>

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.
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:
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) 
{ NullMode(), false); 
if ( || ! 
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)
this.DataContext = null;


This Post Has 0 Comments

  1. Mike

    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.

Leave a Reply