Do all UWP apps leak memory when navigating pages?

前端 未结 4 1485
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 04:18

So I\'ve been getting my teeth into UWP and developing a simple app in C# using VS2017 v15.6.4, on the latest release of Windows 10.

When running the app I notice it

相关标签:
4条回答
  • 2020-12-10 04:48

    I know this is a very old post but I thought it would help to someone. I experienced similar behavior when navigating. but it was always on debug mode. My application even exceed (40Mb before navigations) 100Mb on debug mode when navigating. but NEVER exceeded 1Mb in final release product.

    0 讨论(0)
  • 2020-12-10 05:00

    Yes. This is a bug in UWP. I opened a microsoft support ticket a few month ago and they said last week that they found the bug and solved them. They will ship the fix with the windows insider preview build with the next updates (so i think next week - on the 21.12.2018 it still wasn't included). the fix for everybody will be shipped with the spring update for windows 10.

    0 讨论(0)
  • 2020-12-10 05:07

    I leave my comment here, although it's an old post, for others that might search for it.

    We made huge improvements in memory handeling and GC calls with latest UWP 6.2.9 Nuget update while targeting >=RS3 (Win 10 1709). The complete release notes are here

    0 讨论(0)
  • 2020-12-10 05:13

    In the repro code provided you keep navigating forward, which will create an infinite navigation backstack of page instances (check Frame.BackStack.Count). Since those instances are stored in memory the app's memory usage will naturally grow unbound.

    If you change the code to navigate back to MainPage, and therefore keep the backstack depth at 2, the memory will not grow noticeably with repeated back and forth navigation.

    EDIT However, if we observe this over a longer period of time there is a measurable increase of memory (1.5KB per navigation in my testing). This is a known leak in platform code that has not yet been addressed as of Windows 10 Update 1803.

    The updated version of your test project is shared here:

    Here is the code for NewPage.cs: https://1drv.ms/u/s!AovTwKUMywTNoYVFL7LzamkzwfuRfg

    public sealed partial class NewPage : Page
    {
        DispatcherTimer timer = new DispatcherTimer();
    
        public NewPage()
        {
            InitializeComponent();
            timer.Interval = TimeSpan.FromSeconds(.01);
            timer.Tick += Timer_Tick;
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            timer.Start();
            long managedMem = GC.GetTotalMemory(true);
            ulong totalMem = Windows.System.MemoryManager.AppMemoryUsage;
            System.Diagnostics.Debug.WriteLine(string.Format("Managed Memory: {0} / Total Memory: {1}", managedMem, totalMem));
        }
    
        private void Timer_Tick(object sender, object e)
        {
            timer.Stop();
            Frame.GoBack();
        }
    }
    

    If in your real app you observe a MB size leak after just a couple of navigation cycles then this is not due to above mentioned platform bug, but due to something else that needs to be investigated in the specific app, with the VS memory profiler for example. Often times leaks can be caused due to circular references or static event handlers in the app code. A good first step to debug these would be see if the page's finalizer gets hit as expected when forcing a GC. if not, use the VS memory profiling tools to identify what objects are being leaked and who is holding on to that. That data will help to pinpoint the root cause in the app code for that specific case. Typically those are due to circular references, or static event handlers not being unsubscribed. Happy to help more with this if you can share info from profiling the actual app.

    0 讨论(0)
提交回复
热议问题