Bit Eşlem Belleğini Yönetme

Not: Çoğu durumda, uygulamanızda bit eşlemleri getirmek, kodunu çözmek ve görüntülemek için Glide kitaplığını kullanmanızı öneririz. Kaydırarak, Android'de bit eşlemler ve diğer görsellerle çalışmayla ilgili bu görevleri ve diğer görevleri ele alma karmaşıklığını büyük ölçüde ortadan kaldırabilirsiniz. Glide'ı kullanma ve indirme hakkında bilgi edinmek için GitHub'daki Glide deposunu ziyaret edin.

Bit Eşlemleri Önbelleğe Alma bölümünde açıklanan adımlara ek olarak, çöp toplamayı ve bit eşlem yeniden kullanımını kolaylaştırmak için uygulayabileceğiniz belirli adımlar vardır. Önerilen strateji, hedeflediğiniz Android sürümüne bağlı olarak değişir. Bu sınıfa dahil edilen BitmapFun örnek uygulaması, uygulamanızı Android'in farklı sürümlerinde verimli bir şekilde çalışacak şekilde nasıl tasarlayacağınızı gösterir.

Bu derse zemin hazırlamak için Android'in bit eşlem belleği yönetiminin nasıl geliştiğini aşağıdan inceleyebilirsiniz:

  • Android 2.2 (API düzeyi 8) ve önceki sürümlerde çöp toplama gerçekleştiğinde uygulamanızın iş parçacıkları durdurulur. Bu da performansı düşürebilecek bir gecikmeye yol açar. Android 2.3, eşzamanlı olarak çöp toplama işlemi ekler. Böylece bellek, bit eşlemlere referans verildikten kısa bir süre sonra geri alınır.
  • Android 2.3.3 (API düzeyi 10) ve önceki sürümlerde, bit eşleme için yedek piksel verileri yerel bellekte depolanır. Dalvik yığınında depolanan bit eşlemden ayrıdır. Yerel bellekteki piksel verileri tahmin edilebilir bir şekilde yayınlanmaz. Bu da potansiyel olarak bir uygulamanın bellek sınırlarını kısa süreliğine aşmasına ve kilitlenmesine neden olur. Android 3.0 (API düzeyi 11) ile Android 7.1 (API düzeyi 25) arasındaki piksel verileri, ilişkili bit eşlem ile birlikte Dalvik yığınında depolanır. Android 8.0 (API düzeyi 26) ve sonraki sürümlerde, bit eşlem piksel verileri yerel yığında depolanır.

Aşağıdaki bölümlerde, farklı Android sürümleri için bit eşlem bellek yönetiminin nasıl optimize edileceği açıklanmaktadır.

Android 2.3.3 ve Alt Sürümlerde Belleği Yönetme

Android 2.3.3 (API düzeyi 10) ve önceki sürümlerde recycle() kullanılması önerilir. Uygulamanızda büyük miktarda bit eşlem verisi görüntülüyorsanız muhtemelen OutOfMemoryError hatalarıyla karşılaşırsınız. recycle() yöntemi, bir uygulamanın mümkün olan en kısa sürede belleği geri kazanmasını sağlar.

Dikkat: recycle() öğesini yalnızca bit eşlemin artık kullanılmadığından emin olduğunuzda kullanmalısınız. recycle() yöntemini çağırıp daha sonra bit eşlemi çizmeye çalışırsanız şu hatayı alırsınız: "Canvas: trying to use a recycled bitmap".

Aşağıdaki kod snippet'inde recycle() çağrısına bir örnek verilmiştir. Bir bit eşlemin o anda mı yoksa önbellekte mi görüntülendiğini izlemek için referans sayımını (mDisplayRefCount ve mCacheRefCount değişkenlerinde) kullanır. Şu koşullar karşılandığında kod bit eşlemi geri dönüştürür:

  • Hem mDisplayRefCount hem de mCacheRefCount için referans sayısı 0'dır.
  • Bit eşlem null değil ve henüz geri dönüştürülmedi.

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

Android 3.0 ve Üst Sürümlerde Belleği Yönetme

Android 3.0 (API düzeyi 11), BitmapFactory.Options.inBitmap alanını kullanıma sunuyor. Bu seçenek ayarlanırsa Options nesnesini alan kod çözme yöntemleri, içerik yüklenirken mevcut bir bit eşlemi yeniden kullanmayı dener. Bu, bit eşlemin belleğinin yeniden kullanıldığı ve böylece performansın iyileştirileceği ve hem bellek ayırmanın hem de ayırmanın kaldırılacağı anlamına gelir. Ancak inBitmap ürününün kullanımıyla ilgili belirli kısıtlamalar vardır. Özellikle, Android 4.4 (API düzeyi 19) öncesinde yalnızca eşit boyutlu bit eşlemler desteklenir. Ayrıntılı bilgi için inBitmap belgelerini inceleyin.

Daha sonra kullanmak için bit eşlemi kaydetme

Aşağıdaki snippet, mevcut bir bit eşlemin daha sonra örnek uygulamada kullanılmak üzere nasıl depolandığını göstermektedir. Bir uygulama Android 3.0 veya sonraki bir sürüm üzerinde çalışırken ve LruCache ürününden bir bit eşlem çıkarıldığında, bit eşlem, daha sonra inBitmap ile yeniden kullanılabilmek üzere HashSet öğesine esnek bir şekilde yerleştirilir:

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

Mevcut bir bit eşlemi kullan

Çalışan uygulamada, kod çözücü yöntemleri, kullanabilecekleri mevcut bir bit eşleme olup olmadığını kontrol eder. Örneğin:

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

Sonraki snippet, yukarıdaki snippet'te çağrılan addInBitmapOptions() yöntemini gösterir. inBitmap değeri olarak ayarlanacak mevcut bir bit eşlem arar. Bu yöntemin, yalnızca uygun bir eşleşme bulursa inBitmap için değer ayarladığını unutmayın (kodunuz hiçbir zaman bir eşleşme bulunacağını varsaymamalıdır):

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

Son olarak bu yöntem, aday bit eşleminin inBitmap için kullanılacak boyut ölçütlerini karşılayıp karşılamadığını belirler:

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