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

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

بالإضافة إلى الخطوات الموضحة في الصور النقطية للتخزين المؤقت، تكون هناك إجراءات محددة يمكنك اتخاذها لتسهيل جمع البيانات غير المرغوب فيها وإعادة استخدام الصور النقطية. تعتمد الاستراتيجية المقترَحة على الإصدارات التي تستهدفها نموذج التطبيق 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 المختلفة.

إدارة ميزة "الذاكرة" على Android 2.3.3 والإصدارات الأقدم

على نظام التشغيل 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 = ceil((targetOptions.outWidth * 1.0f / targetOptions.inSampleSize).toDouble()).toInt()
        val height = ceil((targetOptions.outHeight * 1.0f / targetOptions.inSampleSize).toDouble()).toInt()
        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 = (int) Math.ceil(targetOptions.outWidth * 1.0f / targetOptions.inSampleSize);
        int height = (int) Math.ceil(targetOptions.outHeight * 1.0f / 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;
}