简体   繁体   中英

App gets killed but onTrimMemory does not get called

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM