title: UWP: How to properly dispose a page / releasing memory efficiently
body: <p>I am really curious to get to know how to clear/releaase/dispose a page properly.
  Many answers were given such as</p>
  
  <blockquote>
    <p>You do not, the garbage collector does it for you when appropriate.</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/43638188/how-to-dispose-current-page-in-uwp">How to dispose current page in UWP</a></p>
  
  <blockquote>
    <p>foreach(var item in rootFrame.BackStack.ToList())
                        rootFrame.BackStack.Remove(item)</p>
  </blockquote>
  
  <p><a href="https://social.msdn.microsoft.com/Forums/exchange/en-US/2584e99b-3047-4d68-b22c-dcefc3ef9b83/uwpcpage-doesnt-destroyunload-itself-after-onnavigatedfrom?forum=wpdevelop" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/exchange/en-US/2584e99b-3047-4d68-b22c-dcefc3ef9b83/uwpcpage-doesnt-destroyunload-itself-after-onnavigatedfrom?forum=wpdevelop</a></p>
  
  <blockquote>
    <p>Also, to minimize the memory allocation, you must override method
    <code>OnNavigatedTo</code> and <code>OnNavigatedFrom</code>.</p>
    
    <p>In <code>OnNavigatedTo</code> method:</p>
    
    <ul>
    <li>Instantiate all memory intensive object or resources</li>
    <li>Add all events handlers you need</li>
    <li>starts all timers, tasks or threads</li>
    </ul>
    
    <p>In <code>OnNavigatedFrom</code>:</p>
    
    <ul>
    <li>Dispose all resources</li>
    <li>Stops all timers, tasks or threads</li>
    <li>Remove references from all heavy objects</li>
    <li>Remove all events handlers</li>
    <li>Only if you really need, call GC.Collect() ove all events handlers Only if
    
    <blockquote>
      <p>you really need, call GC.Collect()</p>
    </blockquote></li>
    </ul>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/33937447/uwp-windows-10-app-memory-increasing-on-navigation">UWP Windows 10 App memory increasing on navigation</a></p>
  
  <p>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.</p>
  
  <p>I´ve set the NavigationCacheMode to disabled on purpose.</p>
  
  <pre><code>       protected override void OnNavigatedFrom(NavigationEventArgs e) {
          ((ActivityViewModel)this.DataContext).OnDestroy();
          this.DataContext = null;
          base.OnNavigatedFrom(e);
      }
  </code></pre>
  
  <p>I´ve actively called the OnDestroy method to my viewmodel (which is absolutely not great), to set heavy objects to null and release events</p>
  
  <pre><code>        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;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 :/ ).</p>
  
  <p>If you need additional Information, or even a reproduced sample, feel free to ask.</p>
  
  <p>Thank you in advance.</p>
  
  <p>Best regards</p>