إدارة ذاكرة الصور النقطية

ملاحظة: في معظم الحالات، ننصحك باستخدام مكتبة Glide لجلب الصور النقطية وفك ترميزها وعرضها في التطبيق. تساعد هذه الميزة في التخلص من معظم التعقيدات التي تواجهها في معالجة هذه الصور وغيرها من المهام المرتبطة بالعمل مع الصور النقطية والصور الأخرى على نظام التشغيل Android. للحصول على معلومات عن استخدام Glide وتنزيله، يُرجى الانتقال إلى مستودع Glide على GitHub.

بالإضافة إلى الخطوات الموضحة في تخزين الصور النقطية في ذاكرة التخزين المؤقت، هناك إجراءات محددة يمكنك تنفيذها لتسهيل جمع البيانات غير الضرورية وإعادة استخدام الصور النقطية. تعتمد الإستراتيجية المقترحة على إصدار(إصدارات) Android التي تستهدفها. يوضّح لك نموذج التطبيق BitmapFun المضمَّن في هذا الصف طريقة تصميم تطبيقك للعمل بكفاءة على إصدارات مختلفة من Android.

تمهيد الطريق لهذا الدرس، وإليك كيفية تطور إدارة Android لذاكرة الصور النقطية:

  • في نظام التشغيل Android 2.2 (المستوى 8 لواجهة برمجة التطبيقات) والإصدارات الأقدم، تتوقف سلاسل المحادثات لتطبيقك عند جمع البيانات غير المرغوب فيها. يؤدي ذلك إلى تأخر قد يؤدي إلى انخفاض مستوى الأداء. يضيف نظام التشغيل Android 2.3 مجموعة بيانات غير مرغوب فيها متزامنة، ما يعني استعادة الذاكرة بعد وقت قصير من عدم الإشارة إلى الصورة النقطية.
  • في نظام التشغيل Android 2.3.3 (المستوى 10 لواجهة برمجة التطبيقات) والإصدارات الأقدم، يتم تخزين بيانات وحدات البكسل الخلفية للصورة النقطية في الذاكرة الأصلية. وهي منفصلة عن الصورة النقطية نفسها، والتي يتم تخزينها في كومة البريد الإلكتروني Dalvik. لا يتم إصدار بيانات البكسل في الذاكرة الأصلية بطريقة يمكن توقعها، ما قد يؤدي إلى تجاوز التطبيق لفترة قصيرة حدود الذاكرة الأصلية وتعطلها. بدءًا من Android 3.0 (المستوى 11 من واجهة برمجة التطبيقات) وحتى Android 7.1 (المستوى 25 من واجهة برمجة التطبيقات)، يتم تخزين بيانات البكسل في مساحة Dalvik مع الصورة النقطية المرتبطة بها. في الإصدار Android 8.0 (المستوى 26 من واجهة برمجة التطبيقات) والإصدارات الأحدث، يتم تخزين بيانات بكسل الصورة النقطية في كومة الذاكرة المؤقتة الأصلية.

توضح الأقسام التالية كيفية تحسين إدارة ذاكرة الصور النقطية لإصدارات Android المختلفة.

إدارة الذاكرة على الإصدار 2.3.3 من نظام التشغيل Android والإصدارات الأقدم

في نظام التشغيل Android 2.3.3 (المستوى 10 لواجهة برمجة التطبيقات) والإصدارات الأقدم، يُنصح باستخدام recycle(). إذا كنت تعرض كميات كبيرة من بيانات الصور النقطية في تطبيقك، من المحتمل أن تواجه أخطاء OutOfMemoryError. تسمح طريقة recycle() للتطبيق باستعادة الذاكرة في أقرب وقت ممكن.

تنبيه: يجب عدم استخدام recycle() إلا إذا كنت متأكدًا من أنّ الصورة النقطية لم تعد مستخدمة. إذا اتصلت بـ recycle() وحاولت رسم الصورة النقطية لاحقًا، سيظهر لك الخطأ: "Canvas: trying to use a recycled bitmap".

يقدم مقتطف الرمز التالي مثالاً على استدعاء recycle(). وهو يستخدم حساب المراجع (في المتغيرين mDisplayRefCount وmCacheRefCount) لتتبع ما إذا كانت الصورة النقطية يتم عرضها حاليًا أو في ذاكرة التخزين المؤقت. ويعيد الرمز تدوير الصورة النقطية عند استيفاء هذه الشروط:

  • وعدد المراجع لكل من mDisplayRefCount وmCacheRefCount هو 0.
  • الصورة النقطية ليست null، ولم تتم إعادة تدويرها بعد.

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 والإصدارات الأحدث

يقدّم الإصدار Android 3.0 (المستوى 11 لواجهة برمجة التطبيقات) الحقل BitmapFactory.Options.inBitmap. في حال ضبط هذا الخيار، ستحاول طرق فك ترميز المحتوى التي تستخدم الكائن Options إعادة استخدام صورة نقطية حالية عند تحميل المحتوى. وهذا يعني إعادة استخدام ذاكرة الصورة النقطية، ما يؤدي إلى تحسين أدائها وإزالة كل من تخصيص الذاكرة وإلغاء تخصيصها. مع ذلك، هناك قيود معيّنة بشأن كيفية استخدام علامة inBitmap. وعلى وجه الخصوص، قبل الإصدار Android 4.4 (المستوى 19 لواجهة برمجة التطبيقات)، لا تتم إتاحة سوى الصور النقطية ذات الحجم المتساوي فقط. للحصول على التفاصيل، يُرجى الاطّلاع على مستندات inBitmap.

حفظ صورة نقطية لاستخدامها لاحقًا

يوضح المقتطف التالي كيفية تخزين صورة نقطية حالية للاستخدام لاحقًا في نموذج التطبيق. عند تشغيل تطبيق على الإصدار Android 3.0 أو الإصدارات الأحدث وإخراج صورة نقطية من LruCache، يتم وضع مرجع مرن إلى الصورة النقطية في HashSet، لإعادة استخدامها لاحقًا باستخدام 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()));
            }
        }
    }
....
}

استخدام صورة نقطية حالية

في التطبيق قيد التشغيل، تتحقق طرق فك الترميز لمعرفة ما إذا كانت هناك صورة نقطية حالية يمكنهم استخدامها. مثلاً:

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

يعرض المقتطف التالي طريقة addInBitmapOptions() التي يتم استدعاؤها في المقتطف أعلاه. تبحث عن صورة نقطية حالية لضبطها كقيمة لـ inBitmap. لاحظ أن هذه الطريقة لا تحدد قيمة لـ inBitmap إلا إذا عثرت على مطابقة مناسبة (لن تفترض التعليمة البرمجية مطلقًا أنه سيتم العثور على مطابقة):

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

أخيرًا، تحدد هذه الطريقة ما إذا كانت الصورة النقطية المرشحة تفي بمعايير الحجم التي سيتم استخدامها مع 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;
}