glide icon indicating copy to clipboard operation
glide copied to clipboard

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

Open AndroidDeveloperLB opened this issue 9 months ago • 3 comments

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<Bitmap> = 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.

AndroidDeveloperLB avatar May 04 '25 19:05 AndroidDeveloperLB

It seems what I suggested as workarounds don't really work. So now I wonder if there is any way to use Glide just for decoding with extra functionality, or I have to use real targets and have a hard reference to them all the time...

And still, the request is to be able to avoid this auto-recycling easily, without workarounds...

BTW, the docs about recycling a Bitmap says:

This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

https://developer.android.com/reference/android/graphics/Bitmap#recycle()

AndroidDeveloperLB avatar May 05 '25 18:05 AndroidDeveloperLB

You can customize the BitmapPool to avoid bitmap recycling.

821938089 avatar May 10 '25 14:05 821938089

@821938089 Can it work for a specific usage (in specific place in my code) of Glide, and not global for all places?

Can you please demonstrate how it's done?

Wouldn't it be nicer to have a function we call, so that it could be like this, for example (added "setAutoRecyclingEnable") :

 val bitmap =Glide.with(applicationContext).asBitmap()
        .load(file)
        .apply(RequestOptions.centerCropTransform().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE))
        .setAutoRecyclingEnable(false)
        .submit(photoThumbnailSize, photoThumbnailSize).get()

AndroidDeveloperLB avatar May 10 '25 15:05 AndroidDeveloperLB