views:

50

answers:

2
+2  Q: 

WPF Performance

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.

+1  A: 

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...

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelwidth.aspx

also you might give http://stackoverflow.com/questions/185648/dispose-of-image-in-wpf-in-listbox-memory-leak a shot!

santa
Thanks Santa. I will have a look at the converters. I checked virtualization, in fact if I reduce the height of listbox so that only 2 images are visible, the initial memory usage is 144 MB and on every down scroll (which displays next two images) memory increases by 40 MB.
thewpfguy
Thanks. I just tried with DeocodePixelWidth, and it works quite well.
thewpfguy
+1  A: 

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.

Will Dean
Thank you so much. I just did a test with .bmp which are twice the size of jpeg, and the results are amazing. Not only the images loaded in a snap but the UI (scrolling) is also more responsive. Just to add, I did the same test in WinForms and the results are similar, so its not a WPF issue.-> I wonder how the full blown photo gallery apps do the trick.
thewpfguy