title: How to force Universal Application to release memory?
body: <p>Created a simple app that leaks memory:</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;Page
     ...
      x:Class="App3.MainPage"
      mc:Ignorable="d"&gt;
      &lt;StackPanel&gt;
          &lt;Image Width="300" Height="200" 
              Source="/Assets/BBlHOiv.jpg"&gt;&lt;/Image&gt;
          &lt;Button Click="Button_Click"&gt;Navigate&lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p><strong>Code Behind</strong></p>
  
  <pre><code>public sealed partial class MainPage : Page
  {
      public MainPage()
      {
          this.InitializeComponent();            
      }
  
      private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
      {
          this.Frame.Navigate(typeof(MainPage));
      }
  </code></pre>
  
  <p>Every time the button is clicked, the code navigates to the same page which has an image on it ~400KB.  If I continue to click the button at a quick pace, the memory rises each click, or ever few clicks.  The memory is not reclaimed, or at least not in a timely manner.  In a real app, with more complex pages and more complex navigation (including circular navigation) this can be become a big problem.  Question is, how to force the app to release the memory?  Ideally, once a page is no longer needed/shown.</p>
  
  <p>I've tried the following things with no luck.</p>
  
  <p><strong>#1 Clearing the frame's navigation stack</strong></p>
  
  <pre><code>    protected override void OnNavigatedFrom(NavigationEventArgs e)
      {
          base.OnNavigatedFrom(e);
          this.Frame.BackStack.Clear();
      }
  </code></pre>
  
  <p><strong>#2 When using MVVM, setting the DataContext to null (when datacontext is set in the XAML markup)</strong></p>
  
  <pre><code>    protected override void OnNavigatedFrom(NavigationEventArgs e)
      {
          base.OnNavigatedFrom(e);
          this.DataContext = null;
          this.Frame.BackStack.Clear();
      }
  </code></pre>