title: Memory leak in the simplest WPF UserControl with DependencyProperty
body: <p>So I am trying to create a WPF control that has one new dependency property and uses this DP. The control's XAML is as simple as this:</p>
  
  <pre><code>&lt;UserControl x:Class="DPTest.TestControl"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:local="clr-namespace:DPTest"&gt;
  &lt;TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:TestControl}, Path=TestText}"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>Just a TextBlock displaying the new property. Codebehind declares the dependency property like this:</p>
  
  <pre><code>using System.Windows;
  
  namespace DPTest
  {
    public partial class TestControl
    {
      public static readonly DependencyProperty TestTextProperty = 
        DependencyProperty.Register("TestText", typeof(string), typeof(TestControl), new PropertyMetadata(default(string)));
  
      public TestControl()
      {
        InitializeComponent();
      }
  
      public string TestText
      {
        get =&gt; (string) GetValue(TestTextProperty);
        set =&gt; SetValue(TestTextProperty, value);
      }
    }
  }
  </code></pre>
  
  <p>Everything is ok up to this point. But, being created this control never gets collected by GC. To demonstrate this I use a Window which simply creates tons of TestControls on loading:</p>
  
  <pre><code>using System.Windows;
  
  namespace DPTest
  {
    public partial class MainWindow
    {
      public MainWindow()
      {
        InitializeComponent();
        Loaded += OnLoaded;
      }
  
      private void OnLoaded(object sender, RoutedEventArgs e)
      {
        while (true)
        {
          var uc = new TestControl();
        }
      }
    }
  }
  </code></pre>
  
  <p>Opening this window shows RAM rapidly increasing, while GC firing quite often. Memory profiling shows all the instances of TestControl sitting in RAM.</p>
  
  <p>If I replace binding inside the control with constant value so it is just like this:</p>
  
  <pre><code>&lt;TextBlock Text="hello"/&gt;
  </code></pre>
  
  <p>RAM does not increase and the unused control instances get collected successfully.</p>
  
  <p>Looks like I'm doing something wrong but the example is so simple and common so I got stuck. Please help.</p>