Search code examples
c#imagewindows-phone-8memory-leakslonglistselector

Windows phone 8 Images inside LongListSelector memory leak


I have a LongListSelector which contains a image control which loads a lot of images from the web, this works fine for some time, but after i loaded some images i get out of memory exception. I read other people having the same issue regarding out of memory with a lot of images but still haven't found a solution. I have read that it has something to do with image/BitmapImage cache.

Here is my LongListSelector which contains the image control:

<phone:Pivot Title="MY APPLICATION">
        <!--Pivot item one-->
        <phone:PivotItem Header="Browse">
            <Grid>
                <phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                    <Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
                                        <Image.Source>
                                            <BitmapImage UriSource="{Binding BoxArtFrontThumb}"
                                 CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
                                        </Image.Source>
                                    </Image>
                            </Grid>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>
            </Grid>
        </phone:PivotItem>

In my MainPage.xaml.cs i set the DataContext of my LongListSelector:

llsGameList.DataContext = gd.GetGamesListItems;

And here is the class i use to store my image in:

public class GetGamesList 
{
    public Uri BoxArtFrontThumb { get; set; }
}

Here is the ObservableCollection containing all the images:

 private ObservableCollection<GetGamesList> _GetGamesListItems = new ObservableCollection<GetGamesList>();
    public ObservableCollection<GetGamesList> GetGamesListItems
    {
        get
        {
            return this._GetGamesListItems;
        }
    }

I hope i explained it clearly. I really hope there is someone that can help me fix this memory problem. Thanks.


Solution

  • I know no way to prevent the LongListSelector from leaking memory. However, you can use a small trick to free the memory used by pictures.

    First, create a new class, called SafePicture, and make it inherit from ContentControl. Inside, implement the logic to free the memory used by the bitmap:

    public class SafePicture : System.Windows.Controls.ContentControl
    {
        public SafePicture()
        {
            this.Unloaded += this.SafePictureUnloaded;
        }
    
        private void SafePictureUnloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var image = this.Content as System.Windows.Controls.Image;
    
            if (image != null)
            {
                image.Source = null;
            }
        }
    }
    

    Then, wrap all your pictures using this custom control:

    <phone:Pivot Title="MY APPLICATION">
        <!--Pivot item one-->
        <phone:PivotItem Header="Browse">
            <Grid>
                <phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <my:SafePicture>
                                    <Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
                                        <Image.Source>
                                            <BitmapImage UriSource="{Binding BoxArtFrontThumb}"
                                 CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
                                        </Image.Source>
                                    </Image>
                                </my:SafePicture>
                            </Grid>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>
            </Grid>
        </phone:PivotItem>
    

    Note that the namespace my refers to the assembly you've put the SafePicture in, and must be declared on top of your page:

    xmlns:my="clr-namespace:yourNamespace"