Ffimageloading: Memory Leak when using CarouselView

Created on 31 Aug 2017  路  9Comments  路  Source: luberda-molinet/FFImageLoading

Hello,
i got memory leak on android when using
alexrainman's CarouselView with FFImageLoading.

I got like 100 or more images. After swiping thru Cached Image i got OOM.
My control:











ImageService.Instance.Initialize(new Configuration
{
MaxMemoryCacheSize = 52428800
});

stack trace:
https://pastebin.com/qvQbMdHD

Am I doing something wrong? Shouldn't it just free memory after disappearing?

Android more info needed question

All 9 comments

  • Are you sure memory leaks are related to FFImageLoading? I don't know of any current issues related to memory leaks. It can be Xamarin.Forms / CarouselView issue. The exception might be misleading.
  • Your max memory settings are quite low (only 50MB), that can be one of the reasons you're getting OOM's as memory cache won't work efficiently. I would suggest to use default settings which are quite well optimised.

One more thing DownsampleToViewSize won't work correctly if you don't have fixed image sizes. I would recommend using DownsampleWidth/DownsampleHeight instead. It's mentioned in the WIKI.

Yeah wouldn't trust Xamarin's current CarouselView to not leak memory. It causes leaks for us while using FF. I bet if you convert that to a list view you will stop seeing the leak, that would prove to you that it is not FF but the CarouselView. Thankfully there is going to be a stable version of CarouselView in the near future.

I made some changes to make it as simple as possible and ended up with:

        <ListView ItemsSource="{Binding Items}"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" CachingStrategy="RecycleElement" Grid.Row="1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ffimageloading:CachedImage Source="{Binding Url}" Aspect="AspectFit" VerticalOptions="Center" HorizontalOptions="Center" DownsampleUseDipUnits="True" LoadingPlaceholder="loading.png" ErrorPlaceholder="image_error.png" CacheDuration="{Binding CacheDuration}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Its allocating like 200+mb for my app and then it stop loading new images(on listview rescrolling after few sec "fixes it"). e.g. i got about 50 pictures, (jpg about each 200kb) and it stops loading after about 25, then loads new when scrolled down to like 35)
tried v2.2.9 and v2.2.10 and even 2.1.8 of Xamarin.FFImageLoading.Forms
Tested on Samsung S4 mini and Xperia S

Checked with and without MaxMemoryCacheSize.
I can add, that i got custom HttpClient(for authorization purposes).
another stack trace(with crash on v2.2.9 with ListView):
https://pastebin.com/iW72VSNQ

You didn't enable downsampling, please set DownsampleWidth or DownsampleHeight property too. Could you paste some code for your custom http client? There are many ListView examples in our sample project (which is also Xamarin.Forms app) and there are no leaks there.

My HttpClient uses custom HttpClientHandler which just override base.SendAsyc to add token to header. And I found 2 problems.

  1. Is indeed Carousel not freeing memory.
  2. As i wrote, when i got like 10 images on screen, after viewing 25-30(reaching OOM) it dosnt load one or few of them, and then it starts downloading newer ones, leaving that one(/few) broken.

As I said earlier, the Exception might be misleading. FFImageLoading just informs you that the OOM occured and no more images can be loaded (as application would crash). The reason of memory leaks can be a lot of other things though. You should probably report a bug on CarouselView project page.

It has been reported but Xamarin gave up on that code. They are merging a community project as the new carousel view (https://github.com/xamarin/Xamarin.Forms/pull/853). I haven't tried that project but seems promising.

I'll close this then as it should be handled on the on the CarouselView's / Xamarin.Forms side. Feel free to reopen if you'll have some new informations. Thanks.

Anyway here is a repro for this issue:
https://github.com/7lol/MemoryLeak

Can you tell me if clearing memory like:

            FFImageLoading.ImageService.Instance.InvalidateMemoryCache();
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

should be enough?
Anyway if ill open Listview 2-3 times, and scroll down it crashes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EmilAlipiev picture EmilAlipiev  路  15Comments

vlkam picture vlkam  路  21Comments

renzska picture renzska  路  15Comments

jorgenstorlie picture jorgenstorlie  路  28Comments

stopiccot picture stopiccot  路  17Comments