Reputation: 824
I am building a simple photo gallery application which shows images in a listbox. The xaml is:
<ListBox x:Name="imageList" Margin="10,10" ItemsSource="{Binding}" Height="500">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" HorizontalAlignment="Left"></TextBlock>
<Image Source="{Binding}" Width="100" Height="100" HorizontalAlignment="Center"></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The DataContext set here is a string[] of JPEG image file paths.
When I use 10-11 images with a total size of 11 MB, the total memory usage goes up to 500 MB!!! I am really surprised, as this is just a simple photo viewing app doing nothing else. Running this app makes my machine pretty unusable.
I am using VS 2010 express, .NET 4 on Vista. Can any one please explain what is happening in the background which requires such a huge memory footprint? And what can be done to optimize it?
Thanks in advance.
Upvotes: 3
Views: 313
Reputation: 39500
Don't forget that when you load a compressed image (and a JPEG might be VERY compressed), the memory required to hold it once loaded is almost always based on its uncompressed state.
So it can be very misleading to look at the file sizes and then think about memory - you should look at the image pixel sizes - start with length x width x 4 as a rough rule of thumb - and then reconsider if the memory use is so outrageous.
Upvotes: 1
Reputation: 1003
one key here might be virtualization...
the other key might be the decodepixelwidth field of BitmapImage
so bind your image through a converter, to return a less memoryintense decoded variant of your image...
also you might give Dispose of Image in WPF in Listbox (memory leak) a shot!
Upvotes: 2