question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory Leak In Xamarin Forms CachedImage When Using Grid

See original GitHub issue

Here is the scenario I followed:

Get an image from Android photo gallery as a byte array (possibly a downsampled size of 3MP). Create a CachedImage with DownsampleWidth = 100 and DownsampleUseDipUnits = true. Set the source of the CachedImage as photo.Source = ImageSource.FromStream(() => new MemoryStream(photoByteArray)). Then add this to a List<CachedImage> photoViews list.

After each addition or removal, update the Grid with the following code:

        public void ArrangePhotoViews()
        {
            photosGrid.Children.Clear();
            int row = 0;
            int column = 0;
            for (int i = 0; i < photoViews.Count; i++)
            {
                photosGrid.Children.Add(photoViews[i], column, row);
                if(column == 0)
                {
                    column = 1;
                }
                else if(column == 1)
                {
                    column = 0;
                    row++;
                }
            }
        }

I used Android Studio to track memory usage. Every time ArrangePhotoViews is called, regradless whether it is an addition or a removal operation, memory increases as if every photo in the list is newly being created and added to the grid.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
john-larsoncommented, Oct 28, 2017

I was mistaken to think that Android Monitor’s Garbage Collect button would also trigger a garbage collection on Xamarin (.Net) side. After using GC.Collect() from code, I saw that there was no leak.

0reactions
john-larsoncommented, Dec 12, 2017

@EmilAlipiev I manually forced garbage collection just to test the memory usage, that was not meant for managing caching. As far as I understand, what you are trying to do is to clear the cache. @daniel-luberda has recommended me to use the ImageService.Instance.InvalidateMemoryCache() method of the library for that purpose in another thread(see https://github.com/luberda-molinet/FFImageLoading/issues/782). But I would not want to misguide you as I am not an expert on the library. You can find the documentation here: https://github.com/luberda-molinet/FFImageLoading/wiki

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory Leak In Xamarin Forms CachedImage · Issue #782
I used Android Studio to track memory usage. App starts with 11.66 MB and ends with 14.66 MB. Removing the images has no...
Read more >
Cached image (ffimageloading) with OUT OF MEMORY ...
update: I have Searched so much...Do I need a cache key?? listview · exception · xamarin · xamarin.forms · xamarin.android.
Read more >
How to Prevent Memory Leak and Profiling in Xamarin ...
In this article, you will learn how to prevent memory leak and profiling in Xamarin applications.
Read more >
Recent Threads - Xamarin Community Forums - RSSing.com
ListView memory leaks ... I have a Xamarin Forms application. On the main page there is a ListView, every view cell contains text...
Read more >
Your Xamarin application is probably leaking memory and ...
Your Xamarin application is probably leaking memory and you don't know it. One of the least used Xamarin tools, the Profiler, should actually ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found