ניהול הזיכרון של מפת סיביות (Bitmap)

הערה: ברוב המקרים, מומלץ להשתמש החלקה בספרייה כדי לאחזר, לפענח ו הצגת מפות סיביות באפליקציה. בהחלקה מופשטים רוב התוכן את המורכבות בטיפול משימות אחרות שקשורות לעבודה עם מפות סיביות ותמונות אחרות ב-Android. לקבלת מידע על השימוש בהחלקה וההורדה שלה, אפשר להיכנס אל מאגר גלישה ב-GitHub.

בנוסף לשלבים שמתוארים בשמירת מפות סיביות במטמון, יש דברים ספציפיים שאפשר לעשות כדי להקל על איסוף האשפה ושימוש חוזר במפת סיביות (bitmap). האסטרטגיה המומלצת תלויה בגרסה של מכשירי Android שאליהם אתם מטרגטים. האפליקציה לדוגמה BitmapFun נכללת ב בכיתה הזו תלמדו איך לעצב את האפליקציה כך שתעבוד ביעילות גרסאות שונות של Android.

כדי להכין את השטח לשיעור הזה, כך הניהול של Android הזיכרון של מפת סיביות (bitmap) התפתח:

  • ב-Android 2.2 (רמת API 8) ומטה, באשפה מתבצע איסוף, השרשורים של האפליקציה מופסקים. הדבר גורם לעיכוב עלולה לפגוע בביצועים. ב-Android 2.3 נוספים איסוף אשפה בו-זמנית. כלומר, זמן קצר אחרי שהזיכרון של מפת הביטים כבר לא מובלט, או שהזיכרון חוזר לפעול.
  • ב-Android 2.3.3 (רמת API 10) ומטה, נתוני הפיקסלים לגיבוי מפת הסיביות נשמרת בזיכרון המקורי. הוא נפרד ממפת הביטים עצמה, ששמור בערימה של Dalvik. נתוני הפיקסלים בזיכרון המקורי הם לא מופצות באופן צפוי, דבר שעלול לגרום לאפליקציה כדי לחרוג לזמן קצר ממגבלות הזיכרון שלו ולקרוס. מ-Android 3.0 (רמת API 11) עד Android 7.1 (רמת API 25), נתוני הפיקסלים מאוחסנים ערימה של Dalvik יחד עם מפת הסיביות המשויכת. ב-Android 8.0 (רמת API 26), ונתוני הפיקסלים של מפת הסיביות מאוחסנים בערימה המקורית.

בקטעים הבאים מוסבר איך לבצע אופטימיזציה של זיכרון מפת סיביות (bitmap) לניהול גרסאות שונות של Android.

ניהול הזיכרון ב-Android מגרסה 2.3.3 ומטה

ב-Android 2.3.3 (רמת API 10) ומטה, באמצעות recycle() מומלץ. אם אתם מציגים כמויות גדולות של נתוני מפת סיביות באפליקציה: סביר להניח שתיתקלו OutOfMemoryError שגיאות. השיטה recycle() מאפשרת שימוש באפליקציה כדי לשחזר את הזיכרון כמה שיותר מהר.

זהירות: צריך להשתמש recycle() רק כאשר אתה בטוח מפת סיביות (bitmap) כבר לא בשימוש. אם מתקשרים אל recycle() ואחרי שתנסו לשרטט את מפת הסיביות, תקבלו את השגיאה: "Canvas: trying to use a recycled bitmap".

קטע הקוד הבא מדגים קריאה recycle() היא משתמשת בספירת הפניות (במשתנים mDisplayRefCount ו-mCacheRefCount) כדי לעקוב האם מוצגת כרגע מפת סיביות (bitmap) או במטמון. קוד מיחזור של מפת הסיביות כאשר התנאים הבאים מתקיימים:

  • מספר ההפניות גם בשביל 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 (רמת API 11) מציגה את BitmapFactory.Options.inBitmap השדה הזה. אם האפשרות הזו מוגדרת, מפענחים שיטות שמבוססות על אובייקט אחד (Options) ינסה להשתמש שוב במפת סיביות קיימת במהלך טעינת תוכן. כלומר שנעשה שימוש חוזר בזיכרון של מפת הסיביות, מה שמוביל לביצועים משופרים, להסיר גם את הקצאת הזיכרון וגם את ביטול ההקצאה. אבל יש מגבלות מסוימות לגבי האופן שבו ניתן להשתמש ב-inBitmap. בפרט, לפני Android 4.4 (רמת API 19), יש תמיכה במפות סיביות בגדלים שווים בלבד. פרטים נוספים זמינים במאמר מסמכי תיעוד בנושא inBitmap.

שמירת מפת סיביות (bitmap) לשימוש מאוחר יותר

קטע הקוד הבא מדגים איך מאוחסנת מפת סיביות קיימת לשימוש אפשרי להשתמש בהם מאוחר יותר באפליקציה לדוגמה. כשאפליקציה פועלת ב-Android מגרסה 3.0 ואילך מפת סיביות תוסר מה-LruCache, מוצגת הפניה רכה למפת סיביות (bitmap) ב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);
}

קטע הקוד הבא מציג את ה-method 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;
}