bumptech / bumptech/glide

Request: offer a flag to avoid auto-recycling of a Bitmap instance

Open
#5,506 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

```
implementation("com.github.bumptech.glide:glide:5.0.0-rc01")
ksp("com.github.bumptech.glide:ksp:5.0.0-rc01")
```

I use this simple call to get some Bitmap that I want to use right away in the current thread (and maybe even use it somewhere else), and as I don't want the cache to work here (I replace the same file with a different file later), I also disable it:

```
val bitmap =Glide.with(applicationContext).asBitmap()
.load(file)
.apply(RequestOptions.centerCropTransform().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE))
.submit(photoThumbnailSize, photoThumbnailSize).get()
//use the bitmap right here (not into ImageView)
```

I've noticed that I got some crashes reported here via Crashlytics, saying the bitmap is recycled, so I can't use it...
The reason is that there is no reference anymore to the FutureTask, so Glide is auto-recycling the Bitmap:

https://bumptech.github.io/glide/doc/resourcereuse.html#reference-counting

I might be able to overcome this by copying the result bitmap while also having a reference to the task:

```
val futureTarget: FutureTarget = Glide.with(applicationContext)
.asBitmap()
.load(file)
.apply(RequestOptions.centerCropTransform().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE))
.submit(photoThumbnailSize, photoThumbnailSize)
val bitmap: Bitmap? = futureTarget.get()
//copy the bitmap to a new instance here, instead of using it wherever I wish...
```

But this is a bad workaround as it uses double the memory, and even here I'm not sure it will work. Maybe it will cause GC before I copy the Bitmap as there is no use of the "futureTarget" anymore (optimization by the GC), and recycle the Bitmap?

Anyway, please offer a way to overcome this, to let me do the decoding&downsampling operation without the danger of my bitmap being recycled by Glide.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.