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

查看:17
本文介绍了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,如果设置为必需,页面将被创建一次.如果设置为禁用,则每次都会创建页面,并且我检查了按预期调用终结器.

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 的实例(即使该 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" 属性设置为页面.

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天全站免登陆