Header Content

Android – Simple Tips – Bitmap Memory Leaks

The Problem

Java uses garbage collection to manage memory. This does not mean that memory leaks never happen. They do. Bitmaps are one area where memory leaks can occur when creating and Android application.

What Android Developers Has To Say

“On Android Android 2.2 (API level 8) and lower, when garbage collection occurs, your app’s threads get stopped. This causes a lag that can degrade performance. Android 2.3 adds concurrent garbage collection, which means that the memory is reclaimed soon after a bitmap is no longer referenced

On Android 2.3.3 (API level 10) and lower, the backing pixel data for a bitmap is stored in native memory. It is separate from the bitmap itself, which is stored in the Dalvik heap. The pixel data in native memory is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash. As of Android 3.0 (API Level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap“(http://developer.android.com/training/displaying-bitmaps/manage-memory.html).

A Simple Trick
When switching activities, Bitmaps may need to be recycled and garbaged collected. Below is an example of how I do this in the onStop of an Activity where I have a large list of objects that contain Bitmaps that should be recycled.


protected void onStop(){

for (Object object : objectList) {

if(object.bitmap != null){







This recycles the bitmaps no longer being used and tells the garbage collecter that it should run a garbage collection cycle in the near future.
More examples on how to manage Bitmaps can be found through the following link – http://developer.android.com/training/displaying-bitmaps/manage-memory.html

Comments are closed.

Footer Content