Note that there are some explanatory texts on larger screens.

plurals
  1. POGarbage collection fails to reclaim BitmapImage?
    primarykey
    data
    text
    <p>I have an application(WPF) which creates BitmapImages in huge numbers(like 25000). Seems like framework uses some internal logic so after creation there are approx 300 mb of memory consumed(150 virtual and 150 physical). These BitmapImages are added into Image object and they are added into Canvas. The problem is that when I release all those images memory isn't freed. How can I free memory back?</p> <p>The application is simple: Xaml</p> <pre><code> &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Canvas x:Name="canvas" Grid.ColumnSpan="2"&gt;&lt;/Canvas&gt; &lt;Button Content="Add" Grid.Row="1" Click="Button_Click"/&gt; &lt;Button Content="Remove" Grid.Row="1" Grid.Column="1" Click="Remove_click"/&gt; &lt;/Grid&gt; </code></pre> <p>Code-behind</p> <pre><code> const int size = 25000; BitmapImage[] bimages = new BitmapImage[size]; private void Button_Click(object sender, RoutedEventArgs e) { var paths = Directory.GetFiles(@"C:\Images", "*.jpg"); for (int i = 0; i &lt; size; i++) { bimages[i] = new BitmapImage(new Uri(paths[i % paths.Length])); var image = new Image(); image.Source = bimages[i]; canvas.Children.Add(image); Canvas.SetLeft(image, i*10); Canvas.SetTop(image, i * 10); } } private void Remove_click(object sender, RoutedEventArgs e) { for (int i = 0; i &lt; size; i++) { bimages[i] = null; } canvas.Children.Clear(); bimages = null; GC.Collect(); GC.Collect(); GC.Collect(); } </code></pre> <p>This is a screenshot of ResourceManager after adding images <img src="https://i.stack.imgur.com/tFpUJ.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload