Glide: IllegalStateException: Cannot obtain size for recycled Bitmap

Created on 7 Nov 2017  路  28Comments  路  Source: bumptech/glide

Hi,
we recently updated our app from Glide 3.7.0 to Glide 4.1.1. In the new release, we are getting some Crashes in our Crashlogger with the following Stacktrace:

Stack trace / LogCat:

Fatal Exception: java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@7e65b27[450x450] ARGB_8888
       at com.bumptech.glide.util.Util.getBitmapByteSize(SourceFile:73)
       at com.bumptech.glide.load.engine.prefill.BitmapPreFillRunner.allocate(SourceFile:115)
       at com.bumptech.glide.load.engine.prefill.BitmapPreFillRunner.run(SourceFile:132)
       at android.os.Handler.handleCallback(Handler.java:815)
       at android.os.Handler.dispatchMessage(Handler.java:104)
       at android.os.Looper.loop(Looper.java:207)
       at android.app.ActivityThread.main(ActivityThread.java:5710)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)

So far all of the crashes are on devices running Android 6.0. Do you have any hints what I should look for to find what's causing this issue?

bug

Most helpful comment

getBitmapPool() needs to be called inside a if check !bitmap.isRecycled() because when you see the actual method description it does call recycle() hence resulting crash.

All 28 comments

Unfortunately it can be a challenge to track these errors down.

A few things to check are listed here: https://github.com/bumptech/glide/wiki/Resource-re-use-in-Glide#causes

Is this link still valid, because at the top of the page it says it covers Glide 3 only? We have upgraded to Glide 4, we did not have this issue with Glide 3.7.0.

That page is pretty much the same for v3 and v4. I haven't had the chance to write the v4 equivalent yet, though when I do, it'll probably mostly be a copy/paste.

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

I have a similar bug after upgrading from v3 to v4 with a simple GlideApp.with(this).load(url).into(image_view) 馃槥
OS: Android 7.0

Exception java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@237446a[1080x3641] ARGB_8888
com.bumptech.glide.util.Util.getBitmapByteSize (Util.java:74)
com.bumptech.glide.load.resource.bitmap.LazyBitmapDrawableResource.getSize (LazyBitmapDrawableResource.java:52)
com.bumptech.glide.load.engine.EngineResource.getSize (EngineResource.java:51)
com.bumptech.glide.load.engine.cache.LruResourceCache.getSize (LruResourceCache.java:37)
com.bumptech.glide.load.engine.cache.LruResourceCache.getSize (LruResourceCache.java:11)
com.bumptech.glide.util.LruCache.put (LruCache.java:121)
com.bumptech.glide.load.engine.cache.LruResourceCache.put (LruResourceCache.java:11)
com.bumptech.glide.load.engine.Engine.onResourceReleased (Engine.java:334)
com.bumptech.glide.load.engine.EngineResource.release (EngineResource.java:101)
com.bumptech.glide.load.engine.Engine.release (Engine.java:292)
com.bumptech.glide.request.SingleRequest.releaseResource (SingleRequest.java:337)
com.bumptech.glide.request.SingleRequest.clear (SingleRequest.java:316)
com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle (RequestTracker.java:62)
com.bumptech.glide.RequestManager.untrack (RequestManager.java:462)
com.bumptech.glide.RequestManager.untrackOrDelegate (RequestManager.java:449)
com.bumptech.glide.RequestManager.clear (RequestManager.java:437)
com.bumptech.glide.RequestBuilder.into (RequestBuilder.java:574)
com.bumptech.glide.RequestBuilder.into (RequestBuilder.java:626)
[...]

Maybe it's a bug with the LruCache? Race condition?

So far I haven't found a single thing that could cause this in our code. We are not using custom Bitmap transforms or anything where we would be recycling Bitmaps on our own.

Are you able to reproduce this reliably? Or is this something you see in crash logs?

We are not able to reproduce this reliably. We see it in our crashlogger. In the last 24 hours it happened 23 times for 7 users. All running Android 6. This is with >100k daily active users, so it is by no means a huge issue, but still the crash we see most often.

And is it always that same stack trace? Originating from the prefiller?

Yes. The bitmap size in the exception message differs, that depends on the screen size. I can't imagine that matters, though. the trace is the same.

@Kevinrob

I think I've found a way to reproduce @Thrakbad's issue, but I'm not sure yours is related. If you're not using the prefill APIs, please file a new issue.

@Thrakbad the exception is actually happening in Glide's debug logging here: https://github.com/bumptech/glide/blob/f5d5403ffbe54e9a8190be1a16654494462e5457/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillRunner.java#L115.

You might want to strip Log.d/Log.v from your release apks.

I have a fix for the issue anyway that I'll push shortly.

Great, thanks a lot. We will try that in our next release.

Hello! We suffered from the issue after upgrade from v 4.3.1 to 4.4.0
Fatal Exception: java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@b8f2509[512x512] ARGB_8888 at com.bumptech.glide.util.Util.getBitmapByteSize(Util.java:74) at com.bumptech.glide.load.resource.bitmap.BitmapResource.getSize(BitmapResource.java:52) at com.bumptech.glide.load.engine.EngineResource.getSize(EngineResource.java:57) at com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:39) at com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:13) at com.bumptech.glide.util.LruCache.trimToSize(LruCache.java:179) at com.bumptech.glide.util.LruCache.evict(LruCache.java:187) at com.bumptech.glide.util.LruCache.put(LruCache.java:140) at com.bumptech.glide.load.engine.cache.LruResourceCache.put(LruResourceCache.java:13) at com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:327) at com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:109) at com.bumptech.glide.load.engine.Engine.release(Engine.java:287) at com.bumptech.glide.request.SingleRequest.releaseResource(SingleRequest.java:343) at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:322) at com.bumptech.glide.request.ThumbnailRequestCoordinator.clear(ThumbnailRequestCoordinator.java:133) at com.bumptech.glide.manager.RequestTracker.clearRemoveAndMaybeRecycle(RequestTracker.java:72) at com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle(RequestTracker.java:59) at com.bumptech.glide.RequestManager.untrack(RequestManager.java:579) at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:547) at com.bumptech.glide.RequestManager.clear(RequestManager.java:535) at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:588) at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:640)

@VolodymyrMachekhin That's a different stack trace, please file a new issue if you'd like help.

any solutions?

opps锛寃ithout any solution still Nov 19th 2018?

opps锛寃ithout any solution still Jun 20th 2019?
ps: I use glide 4.9.0

similar problem at version 4.9.0 , but can't reproduce crash again

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.timhuang.iifrienddebugrev, PID: 13894
java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@ccba2e4[2160x3840] ARGB_8888
at com.bumptech.glide.util.Util.getBitmapByteSize(Util.java:78)
at com.bumptech.glide.load.resource.bitmap.BitmapResource.getSize(BitmapResource.java:55)
at com.bumptech.glide.load.engine.EngineResource.getSize(EngineResource.java:60)
at com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:42)
at com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:13)
at com.bumptech.glide.util.LruCache.put(LruCache.java:124)
at com.bumptech.glide.load.engine.cache.LruResourceCache.put(LruResourceCache.java:13)
at com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:322)
at com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:113)
at com.bumptech.glide.load.engine.Engine.release(Engine.java:286)

@timhuang1018 I have similar issue here, have you solved it?

In my case the problem was I was calling bitmap.recycle() on the image loaded with Glide. After removing recycle call the error vanished ;)

Same issue here :(

java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@15ebd4c[893x403] ARGB_8888
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4604)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4622)
        at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:39)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I had the same problem锛宨t鈥榮 caused by i used glide download bitmap and i call Bitmap.recycle() method. After removing this calling , i am not repeat this problem

getBitmapPool() needs to be called inside a if check !bitmap.isRecycled() because when you see the actual method description it does call recycle() hence resulting crash.

has this been fixed ?

Glide.with(mContext).clear(imageView);
// clear bitmap from image view here

I had the same prolem at version 4.11.0

Caused by java.lang.IllegalStateException
Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@f7f6ca1[1200x576] ARGB_8888
com.bumptech.glide.util.Util.getBitmapByteSize (Util.java:79)
com.bumptech.glide.load.resource.bitmap.BitmapResource.getSize (BitmapResource.java:52)
com.bumptech.glide.load.engine.EngineResource.getSize (EngineResource.java:62)
com.bumptech.glide.load.engine.cache.LruResourceCache.getSize (LruResourceCache.java:40)
com.bumptech.glide.load.engine.cache.LruResourceCache.onItemEvicted (LruResourceCache.java:11)
com.bumptech.glide.util.LruCache.put (LruCache.java:117)
com.bumptech.glide.load.engine.cache.LruResourceCache.put (LruResourceCache.java:11)
com.bumptech.glide.load.engine.Engine.onResourceReleased (Engine.java:396)
com.bumptech.glide.load.engine.EngineResource.release (EngineResource.java:116)
com.bumptech.glide.load.engine.Engine.release (Engine.java:362)
com.bumptech.glide.request.SingleRequest.clear (SingleRequest.java:326)
com.bumptech.glide.manager.RequestTracker.clearAndRemove (RequestTracker.java:72)
com.bumptech.glide.RequestManager.untrack (RequestManager.java:663)
com.bumptech.glide.RequestManager.untrackOrDelegate (RequestManager.java:631)
com.bumptech.glide.RequestManager.clear (RequestManager.java:627)
com.bumptech.glide.RequestManager.onDestroy (RequestManager.java:374)
com.bumptech.glide.manager.ActivityFragmentLifecycle.onDestroy (ActivityFragmentLifecycle.java:65)
com.bumptech.glide.manager.SupportRequestManagerFragment.onDestroy (SupportRequestManagerFragment.java:215)

do not recycle the resource bitmap in onResourceReady()
@Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { //do not do this: resource.recycler() //and not do like this: //var bitmap = resource //.... bitmap.recycler() }

it is useful for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kooeasy picture kooeasy  路  3Comments

ghost picture ghost  路  3Comments

FooBarBacon picture FooBarBacon  路  3Comments

kenneth2008 picture kenneth2008  路  3Comments

mttmllns picture mttmllns  路  3Comments