title: MemoryManagement for ObservableCollections in .Net
body: <p>I am working on a WPF application in which I am working with an ObservableCollection of CustomObject</p>
  
  <pre><code>public ObservableCollection&lt;ProjectsToShow&gt; Projects{get;set;}
  </code></pre>
  
  <p>Definition for ProjectsToShow class</p>
  
  <pre><code>public class ProjectsToShow 
  {
      public ProjectsToShow()
      {
          Wells = new ObservableCollection&lt;WellsToShow&gt;();
      }
  
      public Project ProjectObject { get; set; }
  
      ObservableCollection&lt;WellsToShow&gt; _wells;
  
      public ObservableCollection&lt;WellsToShow&gt; Wells{get;set;}
  }
  </code></pre>
  
  <p>This class initialize a collection for WellsToShow whose definition is</p>
  
  <pre><code> public class WellsToShow 
  {
      public WellsToShow()
      {
          Datasets = new ObservableCollection&lt;DatasetsToShow&gt;();
      }
  
      public Well WellObject { get; set; }
  
      ObservableCollection&lt;DatasetsToShow&gt; _datasets;
      public ObservableCollection&lt;DatasetsToShow&gt; Datasets
      {
          get { return _datasets; }
          set
          {
              _datasets = value;
              NotifyPropertyChanged("Datasets");
          }
      }
  }
  </code></pre>
  
  <p>and one more level like this.</p>
  
  <p>Now I am profiling my application using memory profiler and it keeps adding the objects in the collection to the memory. I was expecting that calling</p>
  
  <pre><code>Projects.Clear();
  </code></pre>
  
  <p>will release all the objects from the memory but it does not work that way. Even I try to set the Projects to null but even that did not work. Objects of WellsToShow and DatasetToShow still hold on to memory. So for testing purpose I try this code</p>
  
  <pre><code>foreach(var project in MainViewModel.Projects)
          {
              foreach(var well in project.Wells)
              {
                  well.Datasets.Clear();                    
              }
              project.Wells.Clear();
          }
          MainViewModel.Projects.Clear();
  </code></pre>
  
  <p>As per memory profiler they are not in the memory anymore. For the record, each time I run the profiler it runs GC.Collect first and then do the profiling.</p>
  
  <p>Can some please explain how this thing works. If this is the correct way to clear the collection then I need to run and fix this thing in all the projects.</p>
  
  <p>Update 1, Binding this collection to view</p>
  
  <p>I am binding my Project property to TreeView control and upon investigating it profiler. This control is keeping hold of my collection, I believed clearing the items source collection should do the job but this is not the case here.</p>
  
  <pre><code>  &lt;TreeView VirtualizingStackPanel.IsVirtualizing="False"                                      ItemsSource="{Binding Projects}"&gt;
             &lt;TreeView.Resources&gt;
                                  &lt;HierarchicalDataTemplate DataType="{x:Type bll:ProjectsToShow}" ItemsSource="{Binding Wells}"&gt;
                                      &lt;TextBlock HorizontalAlignment="Stretch" Text="{Binding ProjectName}" x:Name="TextBlockProject" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"&gt;
                                          &lt;TextBlock.ContextMenu&gt;
                                              &lt;ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"  &gt;
                                                  &lt;MenuItem 
                                                            Command="{Binding FileEditProjectCommand}"&gt;
                                                      &lt;MenuItem.Header&gt;
                                                          &lt;TextBlock Text="{DynamicResource EditProject}"/&gt;
                                                      &lt;/MenuItem.Header&gt;
                                                  &lt;/MenuItem&gt;
  
                                                  &lt;MenuItem Command="{Binding FileDeleteProjectCommand}"&gt;
                                                      &lt;MenuItem.Header&gt;
                                                          &lt;TextBlock Text="{DynamicResource DeleteProject}"/&gt;
                                                      &lt;/MenuItem.Header&gt;
                                                  &lt;/MenuItem&gt;
                                              &lt;/ContextMenu&gt;
                                          &lt;/TextBlock.ContextMenu&gt;
                                      &lt;/TextBlock&gt;
                                  &lt;/HierarchicalDataTemplate&gt;
                                  &lt;HierarchicalDataTemplate DataType="{x:Type bll:WellsToShow}" ItemsSource="{Binding Datasets}"&gt;
                                      &lt;TextBlock HorizontalAlignment="Stretch" Text="{Binding WellName}" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"&gt;
                                          &lt;TextBlock.ContextMenu&gt;
                                              &lt;ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"&gt;
                                                  &lt;MenuItem Command="{Binding FileEditWellCommand}"&gt;
                                                      &lt;MenuItem.Header&gt;
                                                          &lt;TextBlock Text="{DynamicResource EditWell}"/&gt;
                                                      &lt;/MenuItem.Header&gt;
                                                  &lt;/MenuItem&gt;
                                                  &lt;MenuItem  Command="{Binding FileDeleteWellCommand}"&gt;
                                                      &lt;MenuItem.Header&gt;
                                                          &lt;TextBlock Text="{DynamicResource DeleteWell}"&gt;&lt;/TextBlock&gt;
                                                      &lt;/MenuItem.Header&gt;
                                                  &lt;/MenuItem&gt;
                                              &lt;/ContextMenu&gt;
                                          &lt;/TextBlock.ContextMenu&gt;
                                      &lt;/TextBlock&gt;
                                  &lt;/HierarchicalDataTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>Update 2 Screenshot attached</p>
  
  <p><img src="https://i.stack.imgur.com/1do76.png" alt="enter image description here"></p>