2

My app gets killed with OOM but onTrimMemory methods are not called

I use Fresco for image caching, because I have a lot of images on 4.4 devices my app gets killed with OOM. I tried overriding methods like onTrimMemory() from both Activities that display images and Application class where I try to clear cache in order to avoid OOM but my app still crashed. After that I tried setting logs in this methods and I have noticed that even though my app crashes this methods are not called

There isn't much code to show:

override fun onTrimMemory(level: Int) {
    Timber.d("GalleryActivity onTrimMemory %s", level)
    if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
        Fresco.getImagePipeline().clearCaches()
    }
}

Am I doing something wrong or isn't my understanding right? Shouldn't these methods be called when the app is taking too much memory ?

Are there any ways to tackle this problem ? Thanks

Update, this is how the log looks like:

at android.graphics.Bitmap.createBitmap(Bitmap.java:913) at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:834) at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:318) at android.graphics.drawable.LayerDrawable.draw(LayerDrawable.java:916) at com.facebook.drawee.drawable.ForwardingDrawable.draw(ForwardingDrawable.java:185) at com.facebook.drawee.drawable.ScaleTypeDrawable.draw(ScaleTypeDrawable.java:123) at com.facebook.drawee.drawable.FadeDrawable.drawDrawableWithAlpha(FadeDrawable.java:302) at com.facebook.drawee.drawable.FadeDrawable.draw(FadeDrawable.java:289) at com.facebook.drawee.drawable.ForwardingDrawable.draw(ForwardingDrawable.java:185) at com.facebook.drawee.generic.RootDrawable.draw(RootDrawable.java:81)

Uruos
  • 83
  • 8
  • Make sure you are not trying to allocate a huge bitmap. You should investigate if there's a single reason that always causes OOMs. Do you have any logs? – defhlt Jan 31 '19 at 14:02
  • That was not your question, but I noticed in your code that you call `cleanCaches()`. It cleans not only memory cache and disk cache as well, which does not make sense to do on low RAM situation. Instead I recommend that you implement MemoryTrimmableRegistry. See `Using a MemoryTrimmableRegistry' at https://frescolib.org/docs/configure-image-pipeline.html Fresco will register all memory trimmable parts like caches and pools and you job would be to tell you MemoryTrimmableRegistry to call .trim on all registered trimmables when device memory is running low. – defhlt Jan 31 '19 at 14:15
  • @defhlt I addded my crash log – Uruos Jan 31 '19 at 14:40
  • @defhlt I have tried your last suggestion, but there is no improvement, still crashes – Uruos Jan 31 '19 at 17:07

1 Answers1

0

I would suggest to add separate Service into app for these purposes and try to use onLowMemory, onTrimMemory.

On the other hand it's strange that using Fresco leads to OOM crashes. As one of the solution evictFromMemoryCache can be used on devices with mentioned API level if it will not violate your requirements.

Valerii
  • 395
  • 1
  • 2
  • 8
  • Can you give me some tips on how to use evictFromMemoryCache? Assuming I am displaying images in a recyclerView/viewpager how should this method be used? For all loaded images or only for those who went out of screen ?Thx – Uruos Jan 31 '19 at 13:38
  • I would use it for all loaded images. I use the same functionality `skipMemoryCache` from Glide. After enabling it recyclerview will show image thumbnails only for current visible views. Previously shown pictures will be cleared and loaded again from disk cache. You are welcome. – Valerii Jan 31 '19 at 13:45