title: Xamarin memory leak when swapping pages
body: <p>I am having a lot of difficulty with memory leaks using Xamarin. There is one set in particular that I am struggling with. To illustrate this leak, I created the simplest possible program that simply switches between two pages. The code is as follows:</p>
  
  <p>App.xaml.cs:</p>
  
  <pre><code>    using System;
      using Xamarin.Forms;
      using Xamarin.Forms.Xaml;
      using System.Diagnostics;
  
      [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
      namespace MemoryLeak
      {
          public partial class App : Application
          {
              public static App m_app;
  
              MainPage[] pages = new MainPage[2];
  
              public void SetPage(int idx)
              {
                  MainPage page = pages[idx % 2];
  
                  GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                  Debug.WriteLine(string.Format("{1}:mem={0}", System.GC.GetTotalMemory(true), idx));
  
                  page.ShowMemoryUse();
  
                  MainPage = page;
              }
  
              public App()
              {
                  InitializeComponent();
  
                  m_app = this;
  
                  pages[0] = new MainPage(0);
                  pages[1] = new MainPage(1);
  
                  SetPage(0);
              }
  
              protected override void OnStart()
              {
                  // Handle when your app starts
              }
  
              protected override void OnSleep()
              {
                  // Handle when your app sleeps
              }
  
              protected override void OnResume()
              {
                  // Handle when your app resumes
              }
          }
      }
  </code></pre>
  
  <p>MainPage.xaml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
      &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:local="clr-namespace:MemoryLeak"
                   x:Class="MemoryLeak.MainPage"&gt;
  
          &lt;StackLayout&gt;
              &lt;!-- Place new controls here --&gt;
              &lt;Label x:Name="myText" Text="Welcome to Xamarin.Forms!" 
                 HorizontalOptions="Start"
                 VerticalOptions="Center"/&gt;
              &lt;Button Text="Next" Clicked="OnButtonClicked" /&gt;
          &lt;/StackLayout&gt;
  
      &lt;/ContentPage&gt;
  </code></pre>
  
  <p>MainPage.xaml.cs:</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using Xamarin.Forms;
      using System.Diagnostics;
  
      namespace MemoryLeak
      {
          public partial class MainPage : ContentPage
          {
              int m_id;
  
              public MainPage(int id)
              {
                  InitializeComponent();
  
                  m_id = id;
              }
  
              public void ShowMemoryUse()
              {
                  myText.Text = string.Format("{1}: {0}", System.GC.GetTotalMemory(true), m_id);
              }
  
              void OnButtonClicked(object sender, EventArgs args)
              {
                  App.m_app.SetPage(m_id == 0 ? 1 : 0);
              }
          }
      }
  </code></pre>
  
  <p>Now if I run this program under UWP and click repeatedly on the 'Next' button, I see that the memory usage increases without bounds. If I use the Performance Profiler, I also see numerous objects which are never freed. For example, clicking the 'Next' button 40 times produces the following objects stranded:</p>
  
  <ul>
  <li>PropertyChangedEventHandler - 95 instances created</li>
  <li>EventHandler - 94 instances created</li>
  <li>ConditionalWeakTable - 72 instances created</li>
  <li>Container - 72 instances created</li>
  <li>Xamarin.Forms.Platform.UWP.PageRenderer [RCW] - 41 instances created</li>
  <li>Xamarin.Forms.Platform.UWP.BackgroundTracker - 41 instances created</li>
  <li>RuntimeType - 360 instances created</li>
  </ul>
  
  <p>And many more.</p>
  
  <p>I cannot see what I am doing wrong here. Is this a problem with Xamarin? Should I not be swapping pages by assiging to 'MainPage'?</p>
  
  <p>Thanks.</p>