I have a mainpage where I have 3 pivot items and in each one I have a ListBox which is heavy (Around 15Mb each one, is this normal?). When some action happens I navigate in another page where I have another Pivot control with 3 items. So far, everything is fine. When I navigate back to the main page, I can see a difference in used memory +2 to +4mb. And each time I navigate to the new page and then back to main page another 2-4 mb are added to the total used memory. I am pretty sure that there is nothing wrong in my code. Even if there was a memory leak in my code, it wouldn't be so big. This probably has to do with some UI elements that are not released? I manually call the garbage collector onNavigatedFrom and onNavigatedTo in both pages jsut in case, but still the same..开发者_如何学编程
Could this be a memory leak in some control? As I told you, both pages contain pivot items with listboxes with databinding and the data don't change during runtime.
thank you
If any of the controls you are using on the page has a leak then this will cause the entire element tree of the page to leak (because each child keeps a reference to its parent and vice/versa).
Even if your code is clean, your page can leak if you consume a control from an external party that has a leak. In this case you can mitigate the impact by removing the offending element from your tree in OnNavigatingFrom(). This way only that control is leaked, not the entire page.
The Ad Control currently falls into that category. Here is the guidance for that: http://msdn.microsoft.com/en-us/library/gg491975(v=msads.10).aspx
Similar story for the ContextMenu from the SL Toolkit, in case you are using that.
Discussion at http://forums.create.msdn.com/forums/p/76007/466968.aspx was very helpful for my apps.
Things I did & worked (from above link):
- Declare Destructors for all pages (DEBUG only)
#if DEBUG
~MyPageView()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(new System.Action(() =>
{
System.Windows.MessageBox.Show("MyPageView Destructing");
// Seeing this message box assures that this page is being cleaned up
}));
}
#endif
- Add following to App.xaml in CompleteInitializePhoneApplication method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
#if DEBUG
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
#endif
// Other usual stuff in this method
}
- Clear All DataContext's on navigating away from the page
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
// Clear the page datacontext
this.DataContext = null;
// Clear any other datacontexts esp if the life time of the databound objects are different.
MyDownloadProgressBar.DataContext = null;
// Make sure that if there are any references to elements scoped to this page's lifetime are being held by any other global objects, then they should be cleared here
}
As I find more ways / tips to manage memory in my apps, I'll keep updating this answer.
Sorry my English is very bad. Anyway. I have same problem. I solved like this.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
timer.Tick -= timer_Tick;
this.Loaded -= new RoutedEventHandler(timer_Tick);
AnaMenu.cli.GetAboutCompleted -= client_GetAboutCompleted;
}
I used -=
for events. And fixed it
精彩评论