title: Why do I get an OutOfMemoryException when I have images in my ListBox?
body: <p>I want to display all images stored in the Windows Phone 8 photo folder in my custom gallery which uses a <code>ListBox</code> for displaying the images.</p>
  
  <p>The <code>ListBox</code> code is as follows:</p>
  
  <pre><code>    &lt;phone:PhoneApplicationPage.Resources&gt;
          &lt;MyApp:PreviewPictureConverter x:Key="PreviewPictureConverter" /&gt;
      &lt;/phone:PhoneApplicationPage.Resources&gt;
  
      &lt;ListBox Name="previewImageListbox" VirtualizingStackPanel.VirtualizationMode="Recycling"&gt;
          &lt;ListBox.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;VirtualizingStackPanel CleanUpVirtualizedItemEvent="VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1"&gt;
                  &lt;/VirtualizingStackPanel&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ListBox.ItemsPanel&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid&gt;
                      &lt;Image Source="{Binding Converter={StaticResource PreviewPictureConverter}}" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
       &lt;/ListBox&gt;
  </code></pre>
  
  <p>With the following converter:</p>
  
  <pre><code>public class PreviewPictureConverter : System.Windows.Data.IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          PreviewImageItem c = value as PreviewImageItem;
          if (c == null)
              return null;
          return c.ImageData;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>Images are stored in a custom class:</p>
  
  <pre><code>class PreviewImageItem
  {
      public Picture _picture = null;
      public BitmapImage _bitmap = null;
  
      public PreviewImageItem(Picture pic)
      {
          _picture = pic;
      }
  
      public BitmapImage ImageData 
      {
          get
          {
              System.Diagnostics.Debug.WriteLine("Get picture " + _picture.ToString());
              _bitmap = new BitmapImage();
              Stream data = _picture.GetImage();
              try
              {
                  _bitmap.SetSource(data); // Out-of memory exception (see text)
              }
              catch (Exception ex)
              {
                  System.Diagnostics.Debug.WriteLine("Exception : " + ex.ToString());
              }
              finally
              {
                  data.Close();
                  data.Dispose();
                  data = null;
              }
  
              return _bitmap;
          }
      }
  }
  </code></pre>
  
  <p>The following code is used to set the <code>ListBox</code> data source:</p>
  
  <pre><code>private List&lt;PreviewImageItem&gt; _galleryImages = new List&lt;PreviewImageItem&gt;();
  
  using (MediaLibrary library = new MediaLibrary())
  {
      PictureCollection galleryPics = library.Pictures;
      foreach (Picture pic in galleryPics)
      {
          _galleryImages.Add(new PreviewImageItem(pic));
      }
  
      previewImageListbox.ItemsSource = _galleryImages;
  };
  </code></pre>
  
  <p>Finally here is the "cleanup" code:</p>
  
  <pre><code>private void VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1(object sender, CleanUpVirtualizedItemEventArgs e)
  {
      PreviewImageItem item = e.Value as PreviewImageItem;
  
      if (item != null)
      {
          System.Diagnostics.Debug.WriteLine("Cleanup");
          item._bitmap = null;
      }
  }
  </code></pre>
  
  <p>All this works fine but the code crashes with an <code>OutOfMemoryException</code> after a few images (especially when scrolling fast). The method <code>VirtualizingStackPanel_CleanUpVirtualizedItemEvent_1</code> is called regulary (e.g. every 2 or 3 listbox entries) when the <code>ListBox</code> is scrolled.</p>
  
  <p>What's wrong with this sample code?</p>
  
  <p>Why is memory not freed (fast enough)?</p>