How to Use Bitmap Pools in Android?
Last Updated :
28 Jul, 2021
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.
Similar Reads
How to Resize a Bitmap in Android? ImageViews are used within the android application to display different types of images. Many times images are displayed within the ImageView using bitmap instead of drawable files. In this article, we will take a look at How to Resize Bitmap in the android application to resize our image to be disp
3 min read
How to Convert a Vector to Bitmap in Android? A vector is a set of points, lines, and colors associated with any image object defined inside an XML file. All these associated attributes are compiled in real-time to develop an image object. Simply, a vector is a coded representation of an image object. Bitmap, also known as bitmap index or bit a
3 min read
How to Use Memory Heap Dumps Data in Android? When we design an Android application, the most prevalent concern among developers is the program's memory utilization. This is because, Â the majority of people use low-memory devices, if your program uses a lot of memory, you can lose users. The developers are attempting to locate each and every me
6 min read
How to Use Canvas API in Android Apps? Canvas API is also one of the most used in Android. The name of the API itself tells us that the API is being used for drawing on the drawing board. With the help of this API, we can draw different types of shapes and create custom UI components that are not present in Android. In this article, we w
5 min read
How to Use ThreadPoolExecutor in Android? A task queue contains jobs that are waiting to be performed by any of the pool's idle threads. Producers contribute tasks to the queue, while worker threads function as consumers, consuming jobs from the queue whenever an idle thread is ready to conduct a new background operation. But before we dive
6 min read