WP7内存泄漏? - 使用列表框数据绑定在包含数据透视项的页面之间导航

发布于 2024-10-14 19:33:25 字数 382 浏览 2 评论 0原文

我有一个主页,其中有 3 个枢轴项,每个项中都有一个很重的列表框(每个项大约 15Mb,这是正常的吗?)。 当发生某些操作时,我会导航到另一个页面,其中有另一个包含 3 个项目的 Pivot 控件。到目前为止,一切都很好。 当我导航回主页时,我可以看到已用内存有 +2 到 +4mb 的差异。每次我导航到新页面然后返回主页时,总使用内存中就会添加 2-4 mb。我很确定我的代码没有任何问题。即使我的代码中有内存泄漏,也不会那么大。这可能与一些未发布的 UI 元素有关?我在两个页面中手动调用垃圾收集器 onNavieratedFrom 和 onNavigedTo 以防万一,但仍然相同..

这可能是某些控件中的内存泄漏吗?正如我告诉您的,两个页面都包含带有数据绑定列表框的数据透视项,并且数据在运行时不会更改。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-10-21 19:33:25

如果您在页面上使用的任何控件存在泄漏,那么这将导致页面的整个元素树泄漏(因为每个子元素都保留对其父元素的引用,反之亦然)。

即使您的代码是干净的,如果您使用来自存在泄漏的外部方的控件,您的页面也可能会泄漏。在这种情况下,您可以通过在 OnNavigatingFrom() 中从树中删除有问题的元素来减轻影响。这样,只有控制权被泄漏,而不是整个页面。

广告控件目前属于该类别。以下是相关指导:
http://msdn.microsoft.com/en- us/library/gg491975(v=msads.10).aspx

SL Toolkit 中的 ContextMenu 的类似情况(如果您正在使用它)。

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.

暖伴 2024-10-21 19:33:25

讨论位于 http://forums.create.msdn.com/forums/p /76007/466968.aspx 对我的应用程序非常有帮助。

我所做的事情&有效(来自上面的链接):

  • 为所有页面声明析构函数(仅限调试)
#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
  • 在 CompleteInitializePhoneApplication 方法中将以下内容添加到 App.xaml
        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
#if DEBUG
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
#endif
            // Other usual stuff in this method
        }
  • 清除导航离开页面时的所有 DataContext
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
}

当我找到更多方法/提示来管理应用程序中的内存时,我'会持续更新这个答案。

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.

却一份温柔 2024-10-21 19:33:25

抱歉我的英语很差。反正。我有同样的问题。我是这样解决的。

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;
    }

我使用 -= 来处理事件。并修复了它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文