从内存中删除项目/页面/用户控件 [英] Deleting project/pages/usercontrols from memory

查看:68
本文介绍了从内存中删除项目/页面/用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Windows Phone应用程序,不幸的是,由于页面未正确删除,导致内存泄漏.这可以通过使用答案来解决:

I have made a windows phone app, that unfortunately has some memory leak, because pages are not removed correctly. This was solved by using the answer:

删除页面"窗口电话 说明:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
while (App.RootFrame.RemoveBackEntry() != null) ; //line if you navigate without backkey
base.OnNavigatedTo(e);

this.Dispatcher.BeginInvoke(() =>
{
    GC.Collect();
    GC.WaitForPendingFinalizers();

    this.Dispatcher.BeginInvoke(() =>
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();

        this.Dispatcher.BeginInvoke(() =>
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        });
    });

}

这从探查器中删除了页面引用.但是在导航时,内存仍然增加了一些.而且增加的内存来自何方并不明显. 因此,我尝试引入一个空白项目,并以星型导航方式导航到该项目,如图所示:

This removed the page references from the profiler. But the memory still increased some upon navigation. And it was not obvious where the increased memory came from. I therefore Tried introducing a blank project and navigated to that as a star navigation, Illustrated as:

这删除了20 mb以上的视觉和其他元素.因此,这是重要的一步.因此,我想将功能拆分到不同的项目中,以消除不同功能之间的联系,并保持较低的内存使用率.最终得到以下结构:

This made removed upwards of 20 mb, of visuals and other elements. So this was a major step. I therefore wanted to split functionality out onto different projects as to remove any ties between the different functionalities and keep low memory usage. This ended up with the following structure:

  • WebService
  • MainProject
  • SecondaryProject
  • PortableLibrary

我使用可移植库在所有项目之间共享模型. 这影响到在每次导航到新项目时,旧项目中的所有内容均被清除,即,每个项目仅存在一个元素,从而显着改善了内存.但是仍然有一定程度的内存泄漏,但这仅意味着内存的一定增加. 但是记忆从何而来?回到探查器:

I share models between all projects using the Portable Library. This influenced that upon every navigation to a new project everything is cleared in the old project, i.e. only one element of each item exist, major memory improvement. But still some memory leak, however bounded, meaning only a certain increase in memory. But where comes the memory from? Going back into the profiler:

这表明没有专门删除页面,所有元素 .view.都已被一个项目中所有页面的配置中的代码删除.但是内存泄漏是类似的.

This shows that the pages are not removed specifically, all elements .view. would have been removed by the code in the configuration of all pages being in one project. However the memory leak is similar.

问题

  1. 如何从其他项目中删除页面和元素 相同的解决方案?
  2. 如何从内存中删除项目?
  1. How do I remove pages and elements from a different project within the same solution?
  2. How do I remove a project from memory?

这两个项目之间没有共享的信息,除了它们都可以创建LibraryOfModels的版本.因此,没有跨项目边界共享事件,变量或引用. 但是对于我的一生,我无法将其从内存中删除.我想从内存中删除它们,就像顶部的代码片段一样,如果导航仅在一个项目中.由于导航分散在两个项目中,因此存在该问题.但是它在其他方面也有一些内存方面的好处,这就是为什么我想要这种结构.

There is not information shared between the two projects, other than they both can create a version of LibraryOfModels. Therefore no events, variables or references is shared across the project borders. But for the life of me I cannot get it removed from memory. I want to remove them from the memory, as my code snippet in the top does, IF the navigation was only within one project. The issue exists because of the navigation is spread out on two projects. But it has some memory benefits in other areas, which is why I want this structure.

希望有人可以提供帮助. 谢谢!

Hope somebody can help. Thx!

其他测试

我尝试使所有页面成为用户控件,以便避免导航.但是,将它们重新创建几次会导致1-2 Mb的稳定泄漏.因此,该项目的主意仍然是最好的,因为有一个界限.但是仍然没有将其删除的解决方案.

I have tried making all pages usercontrols such that I could avoid navigation. But recreating them a couple of times gav a steady leak of 1-2 Mb. The project idea is therefore still best as there is a bound. But still no solution of getting it removed.

仍然希望使用发光的键盘来吸引骑士;)

Still hoping for the knight with the shiney keyboard ;)

推荐答案

我认为您可以使对象成为单例,并且可以相应地手动清除对象的值. 请参考下面的链接 https://msdn.microsoft.com/en-us/library/ff650316.aspx

I think you can make the object singleton and you can clear the values of objects manually when accordingly. Please refer the link below https://msdn.microsoft.com/en-us/library/ff650316.aspx

这篇关于从内存中删除项目/页面/用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆