Quản lý bộ nhớ bitmap

Lưu ý: Trong hầu hết các trường hợp, bạn nên sử dụng thư viện Glide để tìm nạp, giải mã và hiển thị bitmap trong ứng dụng của mình. Glide đơn giản hoá hầu hết độ phức tạp trong việc xử lý các tác vụ này và những tác vụ khác liên quan đến việc sử dụng bitmap và các hình ảnh khác trên Android. Để biết thông tin về cách sử dụng và tải Glide, vui lòng truy cập vào Kho lưu trữ Glide trên GitHub.

Ngoài các bước được mô tả trong Lưu các bitmap vào bộ nhớ đệm, bạn có thể thực hiện một số việc cụ thể để hỗ trợ việc thu thập rác và sử dụng lại bitmap. Chiến lược đề xuất phụ thuộc vào(các) phiên bản Android mà bạn đang nhắm mục tiêu. Ứng dụng mẫu BitmapFun đi kèm với lớp này cho bạn biết cách thiết kế ứng dụng của mình để hoạt động hiệu quả trên các phiên bản Android khác nhau.

Để chuẩn bị cho bài học này, dưới đây là cách hoạt động quản lý bộ nhớ bitmap của Android đã phát triển:

  • Trên Android 2.2 (API cấp 8) trở xuống, khi thu thập rác, các luồng của ứng dụng sẽ dừng lại. Điều này gây ra độ trễ có thể làm giảm hiệu suất. Android 2.3 thêm tính năng thu thập rác đồng thời, có nghĩa là bộ nhớ sẽ được lấy lại ngay sau khi bitmap không còn được tham chiếu nữa.
  • Trên Android 2.3.3 (API cấp 10) trở xuống, dữ liệu pixel sao lưu cho bitmap được lưu trữ trong bộ nhớ gốc. Giá trị này tách biệt với chính bitmap, vốn được lưu trữ trong vùng nhớ khối xếp Dalvik. Dữ liệu pixel trong bộ nhớ gốc không được phát hành theo cách có thể dự đoán, điều này có thể khiến một ứng dụng vượt quá giới hạn bộ nhớ và gặp sự cố trong một thời gian ngắn. Từ Android 3.0 (API cấp 11) đến Android 7.1 (API cấp 25), dữ liệu pixel được lưu trữ trên vùng nhớ khối xếp Dalvik cùng với bitmap liên kết. Trong Android 8.0 (API cấp 26) trở lên, dữ liệu pixel bitmap được lưu trữ trong vùng nhớ khối xếp gốc.

Các phần sau mô tả cách tối ưu hóa hoạt động quản lý bộ nhớ bitmap cho các phiên bản Android khác nhau.

Quản lý bộ nhớ trên Android 2.3.3 trở xuống

Trên Android 2.3.3 (API cấp 10) trở xuống, bạn nên sử dụng recycle(). Nếu đang hiển thị một lượng lớn dữ liệu bitmap trong ứng dụng của mình, bạn có thể gặp lỗi OutOfMemoryError. Phương thức recycle() cho phép một ứng dụng lấy lại bộ nhớ sớm nhất có thể.

Lưu ý: Bạn chỉ nên sử dụng recycle() khi chắc chắn bitmap đó không còn được dùng nữa. Nếu gọi recycle() và sau đó cố gắng vẽ bitmap, bạn sẽ gặp lỗi: "Canvas: trying to use a recycled bitmap".

Đoạn mã sau đây cung cấp một ví dụ về cách gọi recycle(). Phương thức này sử dụng chức năng đếm tham chiếu (trong các biến mDisplayRefCountmCacheRefCount) để theo dõi việc liệu một bitmap hiện đang được hiển thị hay đang được lưu trong bộ nhớ đệm. Mã này sẽ tái chế bitmap khi đáp ứng các điều kiện sau:

  • Số lượng tham chiếu cho cả mDisplayRefCountmCacheRefCount là 0.
  • Bitmap này không phải là null và chưa được tái chế.

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();
}

Quản lý bộ nhớ trên Android 3.0 trở lên

Android 3.0 (API cấp 11) giới thiệu trường BitmapFactory.Options.inBitmap. Nếu bạn đặt tuỳ chọn này, thì các phương thức giải mã lấy đối tượng Options sẽ tìm cách sử dụng lại một bitmap hiện có khi tải nội dung. Điều này có nghĩa là bộ nhớ của bitmap được sử dụng lại, cải thiện hiệu suất và loại bỏ cả cách phân bổ bộ nhớ và phân bổ lại. Tuy nhiên, có một số hạn chế nhất định với cách sử dụng inBitmap. Đặc biệt, trước Android 4.4 (API cấp 19), chỉ hỗ trợ bitmap có kích thước bằng nhau. Để biết thông tin chi tiết, vui lòng xem tài liệu inBitmap.

Lưu bitmap để sử dụng sau này

Đoạn mã sau minh hoạ cách một bitmap hiện có được lưu trữ để có thể sử dụng sau này trong ứng dụng mẫu. Khi một ứng dụng đang chạy trên Android 3.0 trở lên và một bitmap bị loại khỏi LruCache, một tham chiếu mềm đến bitmap sẽ được đặt trong HashSet, để có thể sử dụng lại sau này với 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()));
            }
        }
    }
....
}

Sử dụng một bitmap hiện có

Trong ứng dụng đang chạy, các phương thức bộ giải mã sẽ kiểm tra xem có sẵn bitmap có thể sử dụng hay không. Ví dụ:

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);
}

Đoạn mã tiếp theo hiển thị phương thức addInBitmapOptions() được gọi trong đoạn mã trên. Hệ thống sẽ tìm một bitmap hiện có để đặt làm giá trị cho inBitmap. Lưu ý là phương thức này chỉ đặt giá trị cho inBitmap nếu tìm thấy kết quả phù hợp (mã của bạn không bao giờ được giả định sẽ tìm thấy kết quả trùng khớp):

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;
}

Cuối cùng, phương thức này xác định liệu bitmap đề xuất có đáp ứng tiêu chí về kích thước sẽ được sử dụng cho inBitmap hay không:

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;
}