locked
[UWP] BitmapImage memory leaks RRS feed

  • Question

  • Hi all,
    I have an issue with BitmapImage API. I would like to load images in a ListView, but it seems that BitmapImage (or SoftwareImage) creates memory leaks.

    The scenario is the following:
    1 - I convert base 64 string to byte array

    2 - I add this byte array 50 time, using a new BitmapImage for each entry.
    -> This increases memory usage (anomalously?)

    3 - I scroll my listview to bottom
    -> This decreases memory usage

    4 - I add this byte array 50 time, using a new BitmapImage for each entry.
    -> This increases memory usage (anomalously?)

    5 - I Clear listview
    -> This decreases memory usage.

    6 - I save 50 images as storage files using the previous byte array.
    -> In my file system, all files have the size of 16 KB.

    7 - I load all images from storage file to SoftwareBitmap and I add them in the list.
    -> This increases memory usage too.

    8 - I scroll the listview to bottom.
    -> This has no effect.

    9 - I Clear listview
    -> This decreases memory usage.

    I don't know what is the reason of these memory leaks. You can see the diagnostic of memory usage and download my sample project on my OneDrive: https://1drv.ms/f/s!AhumbeBNmlCUgb9GdvRaxdtUTRJoug

    Thanks for help ;-)





    • Edited by Samuel LIOULT Thursday, September 8, 2016 4:17 PM
    • Edited by Martin Zhema Friday, September 9, 2016 4:52 AM modify tag
    Thursday, September 8, 2016 4:13 PM

Answers

All replies

  • Hi Samuel LIOULT,

    In your "#region Second part",  I wrote the following code;

      

     private async void BtnLoadImages_Click(object sender, RoutedEventArgs e)
            {
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                folder = await folder.CreateFolderAsync("images", CreationCollisionOption.OpenIfExists);
                var files = await folder.GetFilesAsync();
                foreach (var nicolas in files)
                {
                    BitmapImage bitmapImage = new BitmapImage();
    
                    using (IRandomAccessStream stream = await nicolas.OpenAsync(FileAccessMode.Read))
                    {
                        bitmapImage.DecodePixelWidth = 500;
                        await bitmapImage.SetSourceAsync(stream);
                    }
                    listView.Items.Add(bitmapImage);
                }
            }

    Then the memory will be decreased when I scroll the ListView, this works the same as your  "#region First part".

    I don't think there exist some leaks in SoftwareBitmapSource, because when I scroll the ListView, the memory should not be changed. So think, there may some optimization when you use the BitmapImage in ListView.


    Best Regards!
    Martin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, September 9, 2016 9:57 AM
  • Hi Martin Zhema,

    I have the same result in my side.

    But I just resolved my issue using this part of the doc:

    https://msdn.microsoft.com/en-us/windows/uwp/debug-test-perf/optimize-animations-and-media#optimize-image-resources

    Thanks


    Friday, September 9, 2016 10:01 AM
  • Hi Samuei,

    I have got it, thanks for your sharing and thanks for marking it as answer, it will make answer searching in the forum easier and be beneficial to other community members as well.


    Best Regards!
    Martin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Martin Zhema Tuesday, September 13, 2016 12:42 PM
    Tuesday, September 13, 2016 12:30 PM