The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP] Page navigation memory leak RRS feed

  • Question

  • Hello,

    I have a real problem with the navigation caching in uwp.
    Every change of the page will be cached in memory and after some clicks it will reach around 500mb!!!

    I am changing page simply with this command:

    Frame.Navigate(typeof(Page), this);

    Basically the frame itself stores a Back and Forwardstack, which is getting erased every time.
    Setting the CacheSize back to 0 won´t effect to this issue.

    This is what happen after navigate around 20 times.

    After a while, the GC will collect "some" snippets... but not the created page instances


    How do you avoid this memory leak ?


    • Edited by Barry Wang Monday, July 11, 2016 7:50 AM title tag
    Friday, July 8, 2016 5:29 PM

All replies

  • This is a snapshot of my used memory

    As you can see, the application is just increasing the total memory usage without wiping some "old" data


    • Edited by Dogzilla Friday, July 8, 2016 5:50 PM
    Friday, July 8, 2016 5:49 PM
  • Hello Dogzilla,

    I cannot reproduce this problem when using a simple page so I guess this problem is special. Can you give us a repro sample or can you check whether you have any specific code which may related to this memory problem. For example, please check whether there are loops in your code to create some UI elements.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 11, 2016 8:11 AM
  • Hello Barry,

    thank you for your reply.

    I have a litte "test" project here which contains the same issue.
    This is a Xamarin project which contains an UWP app.

    When you start navigating through the primary and secondary pages from the hamburger menu, you will notice that the memory will increase every time.

    At the moment, there are two pages. When you hit the plus button at the home frame, you can add some entries to the listview.
    This application was created by the hamburger template from the Template10 framework.

    I really hope you can tell me the problem. I am not perfect. But every step could get me closer :D 



    *https://1drv.ms/u/s!AhVQDy2oIeJFhZNGicYTLs6BfD5d3w

    Wednesday, July 20, 2016 7:57 AM
  • Frame.Navigate(typeof(Page), this);
    This is your problem. You are passing "this", which is a page, to another page as a navigation param. This will make the new page hold a strong reference to your old page, preventing the old one from being removed from cache. This in turn creates a linked list of pages that can never be cleared. You should rethink your application.

    • Edited by mcosmin Wednesday, July 20, 2016 1:20 PM
    • Proposed as answer by Barry Wang Friday, July 22, 2016 1:50 AM
    • Unproposed as answer by Dogzilla Saturday, July 23, 2016 9:16 PM
    Wednesday, July 20, 2016 1:19 PM
  • Hello Barry,

    I´ve already tried your solution. But unfortunately it doesn´t work. I´ve unsubscripted every event, cleared every view (... = null) and using parameterless code.
    He is not able to collect the Page instance for some specific reason.

    I´ve uploaded a Template10 project in my latest answer.
    https://1drv.ms/u/s!AhVQDy2oIeJFhZNGicYTLs6BfD5d3w

    I was able to reproduce the issue once again and I´m not able to handle this problem.

    Thank you in advance


    • Edited by Dogzilla Saturday, July 23, 2016 9:17 PM
    Saturday, July 23, 2016 9:16 PM
  • @Dogzilla,

    Can you share the steps in detail. I ran your sample but I cannot follow you. I haven't reproduced your issue and I cannot see any navigation.

    By the way, if you are using T10 what about add ClearCache="True" in your HamburgetButtonInfo.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 28, 2016 8:52 AM
  • Hi Barry,

    I have noticed similar leak, it leaks only if target build is 14393, works ok in 10586. VS2015 SP3.

    Nothing special in the code, just few Mb DataContext and ordinary Frame.Navigate with null as parameter. NavigationCacheMode=Disabled

    Had to roll back to 10586 for now, can you please give it a try?

    Thank you in advance

    Kind regards

    Vitaly Knyazev


    Sunday, August 21, 2016 10:21 PM
  • Here is the demo project 

    https://github.com/MadMan79/LeakingUwp

    Thursday, September 22, 2016 8:59 PM
  • Hi Vitaly,

    thank you for providing the repro project. I have reproduced the leak locally and am following up the right team on this issue.

    Thanks,
    Stefan Wick
    Windows Developer Platform Team



    Friday, September 23, 2016 6:16 PM
  • I confirm the issue, fortunately, there is a workaround:

    1) Set NavigationCacheMode="Enabled", yes the cache is necessary to avoid memory leak

    2)Override OnNavigatedFrom and inside it, dispose all resources, remove all events, stop all timers and background threads, remove references to heavy objects, and only if necessary call GC.collect

    3) Move all initialization code to OnNavigatedTo method

    Saturday, September 24, 2016 10:00 AM
  • Looks like I have a similar issue. I prepared a minimal sample as well (I can upload to somewhere else if you prefer):

    http://s000.tinyupload.com/index.php?file_id=45866290168650138434

    The sample is very easy:

    • There are 2 pages: MainPage and ExamplePage. You can go from MainPage to ExamplePage and you can go back from ExamplePage to MainPage.
    • Every time you navigate to ExamplePage, a new ID is given to that newly created page (page is not cached).
    • A Grid in ExamplePage emits LayoutChanged event. And event handler writes a text to Debug console like: "grid layout updated on page 0". 0 is the page ID I gave to that page.
    • If you go back and forth a few times, you will see that the text that is written to the console on every navigation gets longer and longer like this:

      grid layout updated on page 1
      grid layout updated on page 2
      grid layout updated on page 0

      notice that the new texts still include the old pages which are unloaded. The old pages, should not emit any events anymore but they keep emitting events although there is no way to navigate to them anymore and they are unloaded. So they also cause a memory leak, they occupy space in memory.
    PS: You can actually remove the view model which includes the page ID completely and it will still keep writing the texts for the old pages. The reason I put the view model is to see the different page numbers easily.


    Sunday, October 9, 2016 10:37 AM
  • This bug has been fixed for the upcoming Creator's Update. If you can, please try out your scenarios on a current Windows Insider build, which has the fixes.

    Thanks,
    Stefan Wick


    Wednesday, March 22, 2017 2:00 AM
  • I just finished installing Creators Update and running some test apps and I am not convinced this is fixed - at least for my problem.
    I am running my app and navigating between 2 pages - I am not using NavigationCacheMode as it doesn't work well for my app.

    Memory usage keeps increasing as I navigate between the 2 pages - the garbage collector never seems to dispose of the pages.

    Is this the same problem that was supposed to be fixed?


    Thanks


    Question - does your app need to be targeting the new version of Win 10?
    • Edited by Ryan Cairns Wednesday, March 29, 2017 8:07 AM
    Wednesday, March 29, 2017 7:57 AM
  • Hi Stefan,

    I dont see there is much difference after updating to Creator's Update.

    Can we get this checked again? This is crucial for project delivery.

    Regards,

    Rummy


    Rummy

    Tuesday, July 18, 2017 1:31 PM
  • Hi. I'm facing a similar issue. Do you have any way to get out of this memory leak issue?
    Wednesday, October 11, 2017 5:52 AM
  • Hey Stefan, 

    this thread is older than a year, but i have this problem too. I use Windows 1803 (17134.81) I have a very simple UWP app with two pages that navigate between them and i memory increases all the time... Are there any otzer options?

    Nico


    VB.NET & C#.NET

    Friday, June 1, 2018 3:10 PM
  • Hi @Stephan Wick [MSFT]

    Any update about this issue? I have the same problems with the new SDk (1803).


    • Edited by Kmors Tuesday, August 21, 2018 4:21 PM
    Tuesday, August 21, 2018 4:21 PM
  • Yeah, i have an update. I had this issue too and i tried a lot. After all i was very sure that this is a memory leak in the deep of UWP. so i opened a support ticket at Microsoft. I wrote three samples that proof this behavior and microsoft was able to reproduce it either. I'm in contact with Microsoft and they are working on my ticket.

    This memory leak happens on every page/control that is created. The memory of the page/control itself is not released. This happens on every navigation and on every datatemplate (e.g. for list items) that is also created.

    Nico


    VB.NET & C#.NET

    Wednesday, August 22, 2018 7:07 PM
  • @Nico, do you know if that fix is going to be in the October 2018 release 1809?
    Thursday, August 23, 2018 2:24 PM
  • Well, i dont think so. Microsoft told me a few minutes ago that i should use the navigation cache, like it is suggested above. I answered, that this is not a solution, because i always need a new instance of view and viewmodel. If i would use the cache i have to reorganize all my 220 usecases, i have to clear all my editors on the view (or properties in the viewmodel), i have to reset all states manually and i have to clear all collections. This is a lot of work just because the memory of the view is not properly released. 

    I wrote three samples that show the problem and I hope that they will ship my samples now to the UWP-Team to fix it. I have no idea how long this would take. I'm totally sure that this bug is very very hard to fix, because the deep of UWP has a lot of unmanaged references and there will be the problem. 

    Nico


    VB.NET & C#.NET

    Thursday, August 23, 2018 4:40 PM
  • Well, i dont think so. Microsoft told me a few minutes ago that i should use the navigation cache, like it is suggested above. I answered, that this is not a solution, because i always need a new instance of view and viewmodel. If i would use the cache i have to reorganize all my 220 usecases, i have to clear all my editors on the view (or properties in the viewmodel), i have to reset all states manually and i have to clear all collections. This is a lot of work just because the memory of the view is not properly released. 

    I wrote three samples that show the problem and I hope that they will ship my samples now to the UWP-Team to fix it. I have no idea how long this would take. I'm totally sure that this bug is very very hard to fix, because the deep of UWP has a lot of unmanaged references and there will be the problem. 

    Nico


    VB.NET & C#.NET

    • Proposed as answer by CClaudiu MSFT Friday, September 20, 2019 10:15 AM
    • Unproposed as answer by CClaudiu MSFT Friday, September 20, 2019 10:15 AM
    Thursday, August 23, 2018 4:41 PM
  • 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. The complete release notes are covered here github.com/microsoft/dotnet/blob/master/releases/UWP/net-native2.2/README.md
    Friday, September 20, 2019 10:23 AM