Ffimageloading: Question: How to properly clear cache?

Created on 11 Dec 2017  路  6Comments  路  Source: luberda-molinet/FFImageLoading

Hi @daniel-luberda

I need your help to understand how to clear cache in some specific cases? here is my scenario

I have mainpage with 3 horizontal listviews with each datetemplate has CacheImage as CacheType Memory implemented. when i profile after main page is loaded, I reach to 100mb memory usage although I am using Downsampling correctly I believe. If I remove lists I experience around 50mb.
I can even accept 100mb for mainpage but my problem starts after I navigate to page 1 with a single listview with images it increases to 120 mb. How can I dispose or get rid off images loaded on Mainpage? I tried to set null ItemsSource each listview in OnDisappearing method of the mainpage but it didnt help at ll?

```
protected override void OnDisappearing()
{
base.OnDisappearing();

        // in order keep memory usage lower
        listview1.ItemsSource = null;
        listview2.ItemsSource = null;
        listview3.ItemsSource = null;

    }

```

question

All 6 comments

FFImageLoading manages memory usage automatically, you shouldn't worry about anything in most scenarios. If you exceed MaxMemoryCacheSize (configurable), it automatically removes bitmaps that are least used from memory, the rest is managed by OS. You shouldn't worry about memory usage until loading some huge amounts of images or some big size images.

If you still want manually remove an entry from cache you can easily do that with:

await ImageService.Instance.InvalidateCacheEntryAsync("image.png or http://something", Cache.CacheType.Memory, removeSimilar: true);

But I advise you to not do that as it isn't necessary.

Hi @daniel-luberda, I'm using Xamarin.Forms and FFImageLoading (latest versions) for an Android, iOS and UWP app. I'm also using SignaturePad to draw on an existing image and overwrite that image. Then I need to invalidate that image from the cache, to reload the new image. When invalidating one single image like described above, it doesn't get invalidated. When invalidating all images, it does work. But I don't want to invalidate the entire image cache, but only one. I've also called ReloadImage() but that doesn't work either.

EDIT: it only works on iOS. Also setting the CacheDuration to 1 ms doesn't do the trick.

When invalidating one single image like described above, it doesn't get invalidated.

  • Do you invalidate it with param removeSimilar set to true?
  • Do you use custom cache?
  • What is your image source?

@jBijsterboschNL You can also set CacheType to Disk and memory cache won't be used.

@daniel-luberda regarding here, i want to ask 2 question.

1) if I use cachetype memory, does it make sense to use CacheDuration, once i exit the app, memory cache will be release I guess.
2) Beside that if I use CacheDuration set for Memory Cachetype on listview, Does it cause a cycle? because if I understand correctly, cycle is caused when the dependency cannot be GC collected and related object will not be as well. this means that if navigate back from a page, I expect that page is disposed. but if there is cached image in the memory, page might be still in the memory and never disposed. Have you ever checked this with a profiler.

if I use cachetype memory, does it make sense to use CacheDuration, once i exit the app, memory cache will be release I guess.

In that case this is ignored as no disk cache is involved.

I expect that page is disposed. but if there is cached image in the memory, page might be still in the memory and never disposed. Have you ever checked this with a profiler.

It's not how it works. Bitmaps are not related to the Xamarin.Forms at all, they're strictly native objects and OS manages them automatically. Page will be disposed without any issues, but the bitmap would be cached so it could be loaded faster for other image loading tasks. FFImageLoading uses LRU memory cache, so only most used bitmaps are cached, others are freed if memory usage is to high.

Was this page helpful?
0 / 5 - 0 ratings