2

I am really curious to get to know how to clear/releaase/dispose a page properly. Many answers were given such as

You do not, the garbage collector does it for you when appropriate.

How to dispose current page in UWP

foreach(var item in rootFrame.BackStack.ToList()) rootFrame.BackStack.Remove(item)

https://social.msdn.microsoft.com/Forums/exchange/en-US/2584e99b-3047-4d68-b22c-dcefc3ef9b83/uwpcpage-doesnt-destroyunload-itself-after-onnavigatedfrom?forum=wpdevelop

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

In OnNavigatedTo method:

  • Instantiate all memory intensive object or resources
  • Add all events handlers you need
  • starts all timers, tasks or threads

In OnNavigatedFrom:

  • Dispose all resources
  • Stops all timers, tasks or threads
  • Remove references from all heavy objects
  • Remove all events handlers
  • Only if you really need, call GC.Collect() ove all events handlers Only if

    you really need, call GC.Collect()

UWP Windows 10 App memory increasing on navigation

So, in our current project, I´ve just made a test to find out how I can "release" ressources. The short answer is... I couldn´t.

I´ve set the NavigationCacheMode to disabled on purpose.

       protected override void OnNavigatedFrom(NavigationEventArgs e) {
        ((ActivityViewModel)this.DataContext).OnDestroy();
        this.DataContext = null;
        base.OnNavigatedFrom(e);
    }

I´ve actively called the OnDestroy method to my viewmodel (which is absolutely not great), to set heavy objects to null and release events

        public void OnDestroy() {
        this.ActivitySource.FetchRows -= this.ActivitySource_FetchRows;
        this.ActivitySource.GetUniqueValues -= this.ActivitySource_GetUniqueValues;
        this.TaskObjectSource = null;
        this.UnitOfWork.Dispose();
        this.ActivitySource = null;
        this.NavigateToTaskCommand = null;
        this.SelectedItem = null;
    }

Guess what. Each time I´ve reentered the page, the ram will slightly increase. If you would repeat this for x times, your application will unevitable throw OutOfMemoryException.

So I was thinking about this and came up with the conclusion that a page is just a control container and should be cached, whereas your datacontext holds the data information which drives the ui controls through its bindings. In the end, it makes most sense to me. Why do create a new instance of a page when you can just exchange its datacontext or manipulate properties in its given context. But then, why is the default NavigationCacheMode 'Disabled'? How would Dialogs be managed due to its volatile behavior? The more I think about this, the more questions I have.

How does "the big guys" dealing with this "issue"? Maybe my understanding is wrong, but then there might be sources to get knowledge. From my current perspective, I really enjoy coding with the uwp. But sometimes it makes me feel like certain things are not well optimized or missing (On-Premise Active Directory :/ ).

If you need additional Information, or even a reproduced sample, feel free to ask.

Thank you in advance.

Best regards

M.Kalinski
  • 23
  • 3
  • Can you please provide a simple sample that can be reproduced for us to test? – Faywang - MSFT Jan 28 '20 at 01:51
  • [link](https://wetransfer.com/downloads/6736e7a65ddba26a2021402c72d2ed6120200128155557/667d16) In the project, you can find a navigationview with3 pages. I was just navigating through each of them (around 3 minutes). Of course, this is not a real test, especially the size is not disputable. But the fact that I´ve started the program with around 25mb and just increasing its size to 80 in around 3 minutes makes me nervous. In our current project, we also can see such a slow increament. If we wouldn´t use the "Enabled/Required" Mode, we would have a lot of memory issues to deal with. – M.Kalinski Jan 28 '20 at 16:05
  • Have you received an OutOfMemory exception? Since if only memory increase, it is not a problem. If the memory increases and the GC works, but the memory does not decrease and you receives an exception. In this case, we need to check whether there is something you haven't released. In addition, we do not recommend loading so much data at once, it is better to set NavigationCacheMode as enabled. – Faywang - MSFT Jan 29 '20 at 09:07
  • We havn´t received OutOfMemory exception yet, but setting the NavigationCacheMode to Disabled made it easy to allocate over 1 1/2 GB. I can try to push limits and force it to run "out of memory". Maybe some objects are long-living and persist in Gen2 for a long time. I don´t know. I´ll let you know if I am able to cause outofmemoryexception. On the other hand, loading so much data (especially in a listview) is pointless. We do paging and incremental loading as often as we can. – M.Kalinski Jan 29 '20 at 13:15
  • OK, if you received the exception, please provide more detailed information about it, e.g. the error code or which line of code causes it, etc. – Faywang - MSFT Jan 30 '20 at 08:44

0 Answers0