Exception "Cannot obtain size for recycled Bitmap",support recycled bitmap in LruResourceCache
- Dominant language
- Java
- Stars
- 35k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 8
Description
**Glide Version**:4.11.0
**Integration libraries**:None
**Device/Android Version**:ALL
### Issue details / Repro steps / Use case background
When a bitmap is loaded, then show in screen, it reasonablly to expected hit cache in next load of the same url.
So i set `ActiveResourceRetentionAllowed` to true in my app. But got some crash like:
```java
IllegalStateException: Cannot obtain size for recycled Bitmap
```
It throws when a resource call `#getSize`
I found desc:
>
// The return value of getAllocationByteCount silently changes for recycled bitmaps from the
// internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
// instead assert here.
Resource can remember the size of bitmap to avoid this.
Why not LruResourceCache just abandon a resource when found the bitmap behind is recycled, but throw a exception to crashing app.
### Analysis
Glide has two level memory cache: LruResourceCache and ActiveResources.
When load a bitmap by call `#into()`, resource will be hold in ActiveResources. ActiveResources is actually a weakReference map,if the code outside of glide doesn't hold a strong ref of Target(E.g call `#into` with a view, view will hold target by `setTag`),it will be call gc soon.
When activeResource been gc,the bitmap will gone,will not put into LruResourceCache,unless set `ActiveResourceRetentionAllowed` to true.
### Code
```java
// first load
Glide.with(context)
.load(url)
.into(new SimpleTarget() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
}
});
// some delay
// Thread.sleep(10000)
// second load. Same url, but can't hit memory cache
Glide.with(context)
.load(url)
.into(new SimpleTarget() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
}
});
```
### Question
Why not LruResourceCache just abandon a resource when found the bitmap behind is recycled, but throw a exception to crashing app.
Contributor guide
Assessment
This issue has not been assessed yet.