UWP Windows 10应用程序内存在导航时增加 [英] UWP Windows 10 App memory increasing on navigation

查看:75
本文介绍了UWP Windows 10应用程序内存在导航时增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UWP Windows 10应用程序,并且注意到任务管理器中的内存使用量随着时间的推移而增加.

I have a UWP Windows 10 App and noticed the memory usage in task manager is increasing over time.

我剥离了该应用程序,发现导航页面时内存正在增加.因此,我制作了一个仅需测试几页的简单应用程序,并且此简单应用程序中的内存仍在增加.我有一个MainPage,它可以将页面从Page1导航到Page2,然后返回到计时器.

I stripped the App back and found the memory is increasing when the navigating pages. So I made a simple app with just a few pages to test and the memory is still increasing in this simple App. I have a MainPage that navigates a frame from Page1 to Page2 and back on a timer.

    public sealed partial class MainPage : Page
{

    private DispatcherTimer _timer;

    private bool _page1Showing;
    private bool _timerRunning;

    public MainPage()
    {
        this.InitializeComponent();

        _timer = new DispatcherTimer();
        _timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
        _timer.Tick += _timer_Tick;
    }

    private void _timer_Tick(object sender, object e)
    {
        GC.Collect();

        this.rootFrame.BackStack.Clear();
        this.rootFrame.ForwardStack.Clear();

        if (_page1Showing)
        {
            this.rootFrame.Navigate(typeof(Page2));
            _page1Showing = false;
        }
        else
        {
            this.rootFrame.Navigate(typeof(Page1));
            _page1Showing = true;
        }
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (_timerRunning)
        {
            _timer.Stop();
            _timerRunning = false;
        }
        else
        {
            _timer.Start();
            _timerRunning = true;
        }
    }

}

Page1和Page2是带有背景颜色网格的空页面,因此您可以看到导航.此应用运行时,任务管理器中的内存使用每30分钟增加大约1MB.

Page1 and Page2 are empty pages with a grid with a background color so you can see the navigation. While this App runs the memory usage in task manager increases by around 1MB every 30 mins.

我已经使用VS2015中的内存诊断程序运行了应用程序,托管堆按预期进行:

I have run the App using the memory diagnostics in VS2015 the Managed heap is as expected:

堆总是在增加:

比较堆显示的快照:

我很困惑这些McgInterop对象是什么?以及为什么这样一个简单的应用程序总是会增加内存使用率.我的主应用程序需要运行很长时间(数月以上).感谢任何帮助.

I am confused what these McgInterop objects are? and why such a simple App is always increasing in memory usage. My main App needs to run for a long time (months+). Appreciate any help.

我尝试更改页面NavigationCacheMode,如果将其设置为Required,则页面仅创建一次.如果设置为禁用",则每次都会创建页面,并且我检查了终结器是否按预期方式调用.

I have tried changing the pages NavigationCacheMode, if set to Required the pages are created once. If set to disabled the Pages are created every time, and I checked the finalizer is called as expected.

-

编辑:我添加了一个按钮来启动和停止计时器(已更新上述内容).似乎在计时器运行时,任务管理器中的内存使用量会增加,而在计时器停止时,内存使用量最终会下降.

Edit: I added a button to start and stop the timer (updated the above). It seems that while the timer is running the memory usage in Task manager will increase, when the timer is stopped the memory usage eventually drops.

我测量了任务管理器在一天中大约每2小时启动和停止一次计时器的内存使用情况,如下所示,它逐渐增加然后在某个时候下降:

I measured the memory usage in task manager over a day starting and stopping the timer around every 2 hours as follows, it slowly increases and then drops at some point:

12.5-> 17.1-> 16.7-> 13.9-> 16.8-> 22.5-> 13.6-> 14.6-> 24.9-> 15.2

12.5 -> 17.1 -> 16.7 -> 13.9 -> 16.8 -> 22.5 -> 13.6 -> 14.6 -> 24.9 -> 15.2

所以我想一切都正常吗?但是我不清楚这里发生了什么,为什么会增加这么多?在什么条件下什么时候有空?

So I guess everything is working fine? But I am not clear what is happening here, why is it increasing so much? When is it being free'd under what conditions?

在浏览页面时,系统是否延迟释放内存?用户通常何时会与屏幕互动?

Is the system delaying releasing memory while pages are navigating? when the user would normally be interacting with the screen?

推荐答案

每次导航到Page时,都会创建一个新的Page实例,但是不会丢弃前一个Page(即使该页面已经在导航中)堆栈).

Every time you navigate to a Page, you create a new instance of Page, but the previous Page is not disposed (even if the Page is already in the navigation stack).

要防止同一页面被多次分配,请将NavigationCacheMode="Enabled"属性设置为Page.

To prevent multiple allocation of same page, set NavigationCacheMode="Enabled" attribute to the Page.

此外,为了最大程度地减少内存分配,您必须重写方法OnNavigatedToOnNavigatedFrom.

Also, to minimize the memory allocation, you must override method OnNavigatedTo and OnNavigatedFrom.

OnNavigatedTo方法中:

  • 实例化所有占用大量内存的对象或资源
  • 添加您需要的所有事件处理程序
  • 启动所有计时器,任务或线程

OnNavigatedFrom中:

  • 处置所有资源
  • 停止所有计时器,任务或线程
  • 从所有重物中删除引用
  • 删除所有事件处理程序
  • 仅在确实需要时,才调用GC.Collect()

这篇关于UWP Windows 10应用程序内存在导航时增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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