Mengelola Memori Bitmap

Catatan: Untuk sebagian besar kasus, sebaiknya gunakan library Glide untuk mengambil, mendekode, dan menampilkan bitmap dalam aplikasi. Glide menyederhanakan sebagian besar kompleksitas dalam menangani tugas ini dan tugas lain yang terkait dengan penggunaan bitmap dan gambar lain di Android. Untuk informasi cara menggunakan dan mendownload Glide, buka repositori Glide di GitHub.

Selain langkah-langkah yang dijelaskan dalam Menyimpan Cache Bitmap, ada hal-hal tertentu yang dapat Anda lakukan untuk memfasilitasi pembersihan sampah memori dan penggunaan ulang bitmap. Strategi yang direkomendasikan bergantung pada versi Android mana yang sedang Anda targetkan. Aplikasi contoh BitmapFun yang disertakan dengan class ini menunjukkan cara mendesain aplikasi agar berfungsi secara efisien di berbagai versi Android.

Sebagai pengantar dalam pelajaran ini, berikut ini perkembangan pengelolaan memori bitmap di Android:

  • Di Android 2.2 (API level 8) dan versi lebih rendah, saat pembersihan sampah memori terjadi, thread aplikasi Anda akan dihentikan. Ini menyebabkan jeda yang dapat menurunkan performa. Android 2.3 menambahkan pembersihan sampah memori serentak yang artinya bahwa memori diperoleh kembali segera setelah bitmap tidak direferensikan lagi.
  • Di Android 2.3.3 (API level 10) dan versi lebih rendah, data piksel pendukung untuk bitmap disimpan dalam memori native. Memori ini terpisah dengan bitmap itu sendiri, yang tersimpan dalam heap Dalvik. Data piksel dalam memori native tidak dirilis dengan cara yang dapat diprediksi, yang berpotensi menyebabkan aplikasi melewati batas memorinya dalam waktu singkat lalu error. Dari Android 3.0 (API level 11) hingga Android 7.1 (API level 25), data piksel disimpan di heap Dalvik beserta bitmap terkait. Di Android 8.0 (API level 26), dan versi lebih tinggi, data piksel bitmap disimpan dalam heap native.

Bagian berikut menjelaskan cara mengoptimalkan pengelolaan memori bitmap untuk berbagai versi Android.

Mengelola Memori di Android 2.3.3 dan Versi Lebih Rendah

Di Android 2.3.3 (API level 10) dan versi lebih rendah, penggunaan recycle() direkomendasikan. Jika Anda menampilkan data bitmap dalam jumlah besar di aplikasi, ada kemungkinan Anda mendapatkan error OutOfMemoryError. Metode recycle() memungkinkan aplikasi memperoleh kembali memori secepatnya.

Perhatian: Sebaiknya hanya gunakan recycle() saat Anda yakin bahwa bitmap tidak digunakan lagi. Jika Anda memanggil recycle() kemudian berupaya menggambar bitmap, Anda akan mendapatkan error: "Canvas: trying to use a recycled bitmap".

Cuplikan kode berikut memberi contoh pemanggilan recycle(). Contoh ini menggunakan jumlah referensi (dalam variabel mDisplayRefCount dan mCacheRefCount) untuk melacak apakah bitmap saat ini sedang ditampilkan atau berada dalam cache. Kode akan mendaur ulang bitmap jika kondisi ini terpenuhi:

  • Jumlah referensi untuk mDisplayRefCount dan mCacheRefCount adalah 0.
  • Bitmap tidak bernilai null, dan belum didaur ulang.

Kotlin

private var cacheRefCount: Int = 0
private var displayRefCount: Int = 0
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
fun setIsDisplayed(isDisplayed: Boolean) {
    synchronized(this) {
        if (isDisplayed) {
            displayRefCount++
            hasBeenDisplayed = true
        } else {
            displayRefCount--
        }
    }
    // 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.
fun setIsCached(isCached: Boolean) {
    synchronized(this) {
        if (isCached) {
            cacheRefCount++
        } else {
            cacheRefCount--
        }
    }
    // Check to see if recycle() can be called.
    checkState()
}

@Synchronized
private fun checkState() {
    // If the drawable cache and display ref counts = 0, and this drawable
    // has been displayed, then recycle.
    if (cacheRefCount <= 0
            && displayRefCount <= 0
            && hasBeenDisplayed
            && hasValidBitmap()
    ) {
        getBitmap()?.recycle()
    }
}

@Synchronized
private fun hasValidBitmap(): Boolean =
        getBitmap()?.run {
            !isRecycled
        } ?: false

Java

private int cacheRefCount = 0;
private int displayRefCount = 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) {
            displayRefCount++;
            hasBeenDisplayed = true;
        } else {
            displayRefCount--;
        }
    }
    // 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) {
            cacheRefCount++;
        } else {
            cacheRefCount--;
        }
    }
    // 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 (cacheRefCount <= 0 && displayRefCount <= 0 && hasBeenDisplayed
            && hasValidBitmap()) {
        getBitmap().recycle();
    }
}

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

Mengelola Memori di Android 3.0 dan Versi Lebih Tinggi

Android 3.0 (API level 11) memperkenalkan kolom BitmapFactory.Options.inBitmap. Jika opsi ini disetel, metode dekode yang mengambil objek Options akan berupaya menggunakan ulang bitmap yang ada saat memuat konten. Ini artinya bahwa memori bitmap digunakan ulang, yang mengakibatkan peningkatan performa, serta menghilangkan alokasi dan de-alokasi memori. Namun, ada batasan tertentu terkait cara penggunaan inBitmap. Secara khusus, sebelum Android 4.4 (API level 19), hanya bitmap berukuran sama yang didukung. Untuk detailnya, lihat dokumentasi inBitmap.

Menyimpan bitmap untuk digunakan nanti

Cuplikan berikut menunjukkan cara menyimpan bitmap yang ada untuk kemungkinan digunakan nanti dalam aplikasi contoh. Saat aplikasi berjalan di Android 3.0 atau yang lebih tinggi dan bitmap dikeluarkan dari LruCache, referensi lunak ke bitmap akan ditempatkan di HashSet, untuk kemungkinan digunakan kembali nanti dengan inBitmap:

Kotlin

var reusableBitmaps: MutableSet<SoftReference<Bitmap>>? = null
private lateinit var memoryCache: LruCache<String, BitmapDrawable>
// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    reusableBitmaps = Collections.synchronizedSet(HashSet<SoftReference<Bitmap>>())
}

memoryCache = object : LruCache<String, BitmapDrawable>(cacheParams.memCacheSize) {

    // Notify the removed entry that is no longer being cached.
    override fun entryRemoved(
            evicted: Boolean,
            key: String,
            oldValue: BitmapDrawable,
            newValue: BitmapDrawable
    ) {
        if (oldValue is RecyclingBitmapDrawable) {
            // The removed entry is a recycling drawable, so notify it
            // that it has been removed from the memory cache.
            oldValue.setIsCached(false)
        } else {
            // The removed entry is a standard BitmapDrawable.
            if (Utils.hasHoneycomb()) {
                // We're running on Honeycomb or later, so add the bitmap
                // to a SoftReference set for possible use with inBitmap later.
                reusableBitmaps?.add(SoftReference(oldValue.bitmap))
            }
        }
    }
}

Java

Set<SoftReference<Bitmap>> reusableBitmaps;
private LruCache<String, BitmapDrawable> memoryCache;

// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    reusableBitmaps =
            Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
}

memoryCache = new LruCache<String, BitmapDrawable>(cacheParams.memCacheSize) {

    // Notify the removed entry that is no longer being cached.
    @Override
    protected void entryRemoved(boolean evicted, String key,
            BitmapDrawable oldValue, BitmapDrawable newValue) {
        if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
            // The removed entry is a recycling drawable, so notify it
            // that it has been removed from the memory cache.
            ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
        } else {
            // The removed entry is a standard BitmapDrawable.
            if (Utils.hasHoneycomb()) {
                // We're running on Honeycomb or later, so add the bitmap
                // to a SoftReference set for possible use with inBitmap later.
                reusableBitmaps.add
                        (new SoftReference<Bitmap>(oldValue.getBitmap()));
            }
        }
    }
....
}

Menggunakan bitmap yang ada

Di aplikasi yang sedang berjalan, metode decoder akan memeriksa apakah sudah ada bitmap yang tersedia dan dapat digunakan. Contoh:

Kotlin

fun decodeSampledBitmapFromFile(
        filename: String,
        reqWidth: Int,
        reqHeight: Int,
        cache: ImageCache
): Bitmap {

    val options: BitmapFactory.Options = BitmapFactory.Options()
    ...
    BitmapFactory.decodeFile(filename, options)
    ...

    // If we're running on Honeycomb or newer, try to use inBitmap.
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache)
    }
    ...
    return BitmapFactory.decodeFile(filename, options)
}

Java

public static Bitmap decodeSampledBitmapFromFile(String filename,
        int reqWidth, int reqHeight, ImageCache cache) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    ...
    BitmapFactory.decodeFile(filename, options);
    ...

    // If we're running on Honeycomb or newer, try to use inBitmap.
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }
    ...
    return BitmapFactory.decodeFile(filename, options);
}

Cuplikan berikutnya menunjukkan metode addInBitmapOptions() yang dipanggil dalam cuplikan di atas. Metode ini mencari bitmap yang ada untuk disetel sebagai nilai bagi inBitmap. Perlu diperhatikan bahwa metode ini hanya menetapkan nilai untuk inBitmap jika menemukan kecocokan yang sesuai (kode seharusnya tidak berasumsi bahwa akan ditemukan kecocokan):

Kotlin

private fun addInBitmapOptions(options: BitmapFactory.Options, cache: ImageCache?) {
    // inBitmap only works with mutable bitmaps, so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true

    // Try to find a bitmap to use for inBitmap.
    cache?.getBitmapFromReusableSet(options)?.also { inBitmap ->
        // If a suitable bitmap has been found, set it as the value of
        // inBitmap.
        options.inBitmap = inBitmap
    }
}

// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
fun getBitmapFromReusableSet(options: BitmapFactory.Options): Bitmap? {
    mReusableBitmaps?.takeIf { it.isNotEmpty() }?.let { reusableBitmaps ->
        synchronized(reusableBitmaps) {
            val iterator: MutableIterator<SoftReference<Bitmap>> = reusableBitmaps.iterator()
            while (iterator.hasNext()) {
                iterator.next().get()?.let { item ->
                    if (item.isMutable) {
                        // Check to see it the item can be used for inBitmap.
                        if (canUseForInBitmap(item, options)) {
                            // Remove from reusable set so it can't be used again.
                            iterator.remove()
                            return item
                        }
                    } else {
                        // Remove from the set if the reference has been cleared.
                        iterator.remove()
                    }
                }
            }
        }
    }
    return null
}

Java

private static void addInBitmapOptions(BitmapFactory.Options options,
        ImageCache cache) {
    // inBitmap only works with mutable bitmaps, so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true;

    if (cache != null) {
        // Try to find a bitmap to use for inBitmap.
        Bitmap inBitmap = cache.getBitmapFromReusableSet(options);

        if (inBitmap != null) {
            // If a suitable bitmap has been found, set it as the value of
            // inBitmap.
            options.inBitmap = inBitmap;
        }
    }
}

// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
        Bitmap bitmap = null;

    if (reusableBitmaps != null && !reusableBitmaps.isEmpty()) {
        synchronized (reusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator
                    = reusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap.
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again.
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }
    return bitmap;
}

Terakhir, metode ini menentukan apakah bitmap kandidat memenuhi kriteria ukuran yang akan digunakan untuk inBitmap:

Kotlin

private fun canUseForInBitmap(candidate: Bitmap, targetOptions: BitmapFactory.Options): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        val width: Int = targetOptions.outWidth / targetOptions.inSampleSize
        val height: Int = targetOptions.outHeight / targetOptions.inSampleSize
        val byteCount: Int = width * height * getBytesPerPixel(candidate.config)
        byteCount <= candidate.allocationByteCount
    } else {
        // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
        candidate.width == targetOptions.outWidth
                && candidate.height == targetOptions.outHeight
                && targetOptions.inSampleSize == 1
    }
}

/**
 * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
 */
private fun getBytesPerPixel(config: Bitmap.Config): Int {
    return when (config) {
        Bitmap.Config.ARGB_8888 -> 4
        Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2
        Bitmap.Config.ALPHA_8 -> 1
        else -> 1
    }
}

Java

static boolean canUseForInBitmap(
        Bitmap candidate, BitmapFactory.Options targetOptions) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        int width = targetOptions.outWidth / targetOptions.inSampleSize;
        int height = targetOptions.outHeight / targetOptions.inSampleSize;
        int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= candidate.getAllocationByteCount();
    }

    // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
    return candidate.getWidth() == targetOptions.outWidth
            && candidate.getHeight() == targetOptions.outHeight
            && targetOptions.inSampleSize == 1;
}

/**
 * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
 */
static int getBytesPerPixel(Config config) {
    if (config == Config.ARGB_8888) {
        return 4;
    } else if (config == Config.RGB_565) {
        return 2;
    } else if (config == Config.ARGB_4444) {
        return 2;
    } else if (config == Config.ALPHA_8) {
        return 1;
    }
    return 1;
}