Open In App

How to Use Bitmap Pools in Android?

Last Updated : 28 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

There are a few things you may try if your program exhibits jitters or slowness when scrolling or paging. Which makes you use things like GPU Overdraw and Profile GPU rendering to figure out where the issues are. But sometimes you just cannot figure out the issue of the lag because that is basically due to an image, that too precisely a bitmap. If your Android App uses heavy images or bitmaps then certainly your app will see a huge performance drop and will suffer from unwanted lag which might agitate the user and make him/her uninstall it, something which you wouldn’t want ever. As a result of this whenever the image backstacks, it basically calls the Garbage Collector and calling it frequently causes UI Freeze.

The Solution?

The solution to this issue is rather simple and you might even be using it unknowingly with some modern-day libraries like Glide or Picasso! Glide Bitmap Pool is a memory management library that allows you to reuse bitmap memory. Because it reuses bitmap memory, there is no need to invoke the Garbage Collector repeatedly, resulting in a smoother functioning program. On supported Android versions, it decodes the bitmap using inBitmap. By leveraging the Bitmap pool to minimize constant memory allocation and deallocation in your application, you reduce GC overhead, resulting in a more stable application.

Watch this video from 100 Days of Google devs to understand how to re-use the bitmaps and save your application from crashing/hanging.

Explanation

Assume we need to import 10 bitmaps into our Android app. When we load bitmap one, the memory for bitmap one is allocated. Then, after a while when we no longer require bitmap one, we should not recycle the bitmap (as recycling involves calling GC). Use this bitmap one as an inBitmap for bitmap two instead. The same memory may thus be utilized for bitmap two.

Reusing the Same Bitmap

Let’s apply the above explanation in a real-life Android Project to better understand the reusing concept:

Java




Bitmap gfgBitmap = BitmapFactory.decodeFile(gfgPath);
imageView.setImageBitmap(gfgPath);
  
// Here the gfgPath is the path of 
// the bitmap where it is located.
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePathTwo, options);
  
if (canUseForInBitmap(bitmapOne, options)) {
    options.inMutable = true;
    options.inBitmap = bitmapOne;
    // Setting these options are 
    // the most important part!!!!!
}
  
options.inJustDecodeBounds = false;
Bitmap gfgBitmap2 = BitmapFactory.decodeFile(gfgPath2, options);
imageView.setImageBitmap(gfgPath2);


With this, you can now be reusing the bitmaps without calling the GC repeatedly and hence your android app has enhanced performance and now will not lag due to the reusing of the Bitmaps.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads