page.title=Managing Bitmap Memory parent.title=Displaying Bitmaps Efficiently parent.link=index.html trainingnavtop=true @jd:body

This lesson teaches you to

  1. Manage Memory on Android 2.3.3 and Lower
  2. Manage Memory on Android 3.0 and Higher

You should also read

Try it out

Download the sample

BitmapFun.zip

In addition to the steps described in Caching Bitmaps, there are specific things you can do to facilitate garbage collection and bitmap reuse. The recommended strategy depends on which version(s) of Android you are targeting. The {@code BitmapFun} sample app included with this class shows you how to design your app to work efficiently across different versions of Android.

To set the stage for this lesson, here is how Android's management of bitmap memory has evolved:

The following sections describe how to optimize bitmap memory management for different Android versions.

Manage Memory on Android 2.3.3 and Lower

On Android 2.3.3 (API level 10) and lower, using {@link android.graphics.Bitmap#recycle recycle()} is recommended. If you're displaying large amounts of bitmap data in your app, you're likely to run into {@link java.lang.OutOfMemoryError} errors. The {@link android.graphics.Bitmap#recycle recycle()} method allows an app to reclaim memory as soon as possible.

Caution: You should use {@link android.graphics.Bitmap#recycle recycle()} only when you are sure that the bitmap is no longer being used. If you call {@link android.graphics.Bitmap#recycle recycle()} and later attempt to draw the bitmap, you will get the error: {@code "Canvas: trying to use a recycled bitmap"}.

The following code snippet gives an example of calling {@link android.graphics.Bitmap#recycle recycle()}. It uses reference counting (in the variables {@code mDisplayRefCount} and {@code mCacheRefCount}) to track whether a bitmap is currently being displayed or in the cache. The code recycles the bitmap when these conditions are met:

private int mCacheRefCount = 0;
private int mDisplayRefCount = 0;
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
public void setIsDisplayed(boolean isDisplayed) {
    synchronized (this) {
        if (isDisplayed) {
            mDisplayRefCount++;
            mHasBeenDisplayed = true;
        } else {
            mDisplayRefCount--;
        }
    }
    // Check to see if recycle() can be called.
    checkState();
}

// Notify the drawable that the cache state has changed.
// Keep a count to determine when the drawable is no longer being cached.
public void setIsCached(boolean isCached) {
    synchronized (this) {
        if (isCached) {
            mCacheRefCount++;
        } else {
            mCacheRefCount--;
        }
    }
    // Check to see if recycle() can be called.
    checkState();
}

private synchronized void checkState() {
    // If the drawable cache and display ref counts = 0, and this drawable
    // has been displayed, then recycle.
    if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed
            && hasValidBitmap()) {
        getBitmap().recycle();
    }
}

private synchronized boolean hasValidBitmap() {
    Bitmap bitmap = getBitmap();
    return bitmap != null && !bitmap.isRecycled();
}

Manage Memory on Android 3.0 and Higher

Android 3.0 (API Level 11) introduces the {@link android.graphics.BitmapFactory.Options#inBitmap BitmapFactory.Options.inBitmap} field. If this option is set, decode methods that take the {@link android.graphics.BitmapFactory.Options Options} object will attempt to reuse an existing bitmap when loading content. This means that the bitmap's memory is reused, resulting in improved performance, and removing both memory allocation and de-allocation. There are some caveats in using {@link android.graphics.BitmapFactory.Options#inBitmap}: