Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Using BitmapPool with Glide library

I'm using the glide library in my app for displaying bitmap (from asset and from url). It works well but I get somme Out of memomy issues as I'm displaying a lot of images in each activities. I see I can use the clearMemory() from BitmapPool, but I have no clue how to call it..

Is someone know how to call it?

Thanks

like image 791
Chol Avatar asked Mar 17 '23 11:03

Chol


1 Answers

You can use clearMemory() or trimMemory() to clear both Glide's memory cache and bitmap pool:

Glide.get(context).clearMemory()
// or:
Glide.get(context).trimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);

That said, you shouldn't need to do either. Two things to check:

  1. Are you loading multiple large images? If so, you need to make sure they are downsampled, so consider setting an explicit size on the View you're loading the image into or using Glide's override() API on your request. Doing so in conjunction with a Transformation like fitCenter will help reduce the memory used per image.
  2. Are there memory leaks in your application? Although Bitmap allocation often throws the actual OutOfMemoryException, most of the time the root cause is a memory leak elsewhere in the application. If #1 doesn't solve your problem, try capturing a heap dump and checking for leaks. The Android developer pages has a great tutorial on how to do so.
like image 132
Sam Judd Avatar answered Mar 27 '23 18:03

Sam Judd