Bitmap-Arbeitsspeicher verwalten

Hinweis : In den meisten Fällen empfehlen wir, die Glide-Bibliothek zu verwenden, um Bitmaps in Ihrer App abzurufen, zu decodieren und anzuzeigen. Glide entlastet den Großteil der Komplexität bei diesen und anderen Aufgaben im Zusammenhang mit der Arbeit mit Bitmaps und anderen Bildern unter Android. Informationen zum Verwenden und Herunterladen von Glide finden Sie im Glide-Repository auf GitHub.

Zusätzlich zu den unter Bitmaps zwischenspeichern beschriebenen Schritten können Sie bestimmte Schritte ausführen, um die automatische Speicherbereinigung und die Wiederverwendung von Bitmap-Dateien zu vereinfachen. Die empfohlene Strategie hängt davon ab, welche Android-Version(en) Sie verwenden möchten. Die in dieser Klasse enthaltene Beispiel-App BitmapFun zeigt dir, wie du deine App so entwickelst, dass sie über verschiedene Android-Versionen hinweg effizient funktioniert.

Für den Einstieg in diese Lektion hat sich die Verwaltung des Bitmap-Speichers in Android wie folgt weiterentwickelt:

  • Unter Android 2.2 (API-Level 8) und niedriger werden die Threads Ihrer App gestoppt, wenn die automatische Speicherbereinigung durchgeführt wird. Dies führt zu einer Verzögerung, die die Leistung beeinträchtigen kann. Unter Android 2.3 wird eine gleichzeitige automatische Speicherbereinigung hinzugefügt. Dies bedeutet, dass der Arbeitsspeicher freigegeben wird, kurz nachdem auf eine Bitmap nicht mehr verwiesen wird.
  • Unter Android 2.3.3 (API-Level 10) und niedriger werden die Back-up-Pixeldaten für eine Bitmap im nativen Arbeitsspeicher gespeichert. Sie ist getrennt von der Bitmap selbst, die im Dalvik-Heap gespeichert ist. Die Pixeldaten im nativen Arbeitsspeicher werden nicht auf vorhersehbare Weise freigegeben, was dazu führen kann, dass eine Anwendung kurzzeitig ihre Speicherlimits überschreitet und abstürzt. Von Android 3.0 (API-Level 11) bis Android 7.1 (API-Level 25) werden die Pixeldaten zusammen mit der zugehörigen Bitmap auf dem Davik-Heap gespeichert. Unter Android 8.0 (API-Level 26) und höher werden die Bitmap-Pixeldaten im nativen Heap gespeichert.

In den folgenden Abschnitten wird beschrieben, wie Sie die Bitmap-Arbeitsspeicherverwaltung für verschiedene Android-Versionen optimieren.

Arbeitsspeicher unter Android 2.3.3 und niedriger verwalten

Unter Android 2.3.3 (API-Level 10) und niedriger wird die Verwendung von recycle() empfohlen. Wenn Sie in Ihrer Anwendung große Mengen von Bitmapdaten anzeigen, können OutOfMemoryError-Fehler auftreten. Mit der Methode recycle() kann eine App Arbeitsspeicher so schnell wie möglich freigeben.

Achtung:Verwenden Sie recycle() nur dann, wenn Sie sicher sind, dass die Bitmap nicht mehr verwendet wird. Wenn Sie recycle() aufrufen und später versuchen, die Bitmap zu zeichnen, wird der folgende Fehler angezeigt: "Canvas: trying to use a recycled bitmap".

Das folgende Code-Snippet zeigt ein Beispiel für den Aufruf von recycle(). Dabei wird mithilfe der Referenzzählung (in den Variablen mDisplayRefCount und mCacheRefCount) erfasst, ob eine Bitmap angezeigt wird oder sich im Cache befindet. Wenn diese Bedingungen erfüllt sind, wird die Bitmap mit dem Code wiederverwendet:

  • Die Referenzanzahl für mDisplayRefCount und mCacheRefCount ist 0.
  • Die Bitmap ist nicht null und wurde noch nicht recycelt.

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

Arbeitsspeicher unter Android 3.0 und höher verwalten

In Android 3.0 (API-Ebene 11) wurde das Feld BitmapFactory.Options.inBitmap eingeführt. Wenn diese Option festgelegt ist, versuchen Decodierungsmethoden, die das Options-Objekt verwenden, beim Laden von Inhalten eine vorhandene Bitmap wiederzuverwenden. Das bedeutet, dass der Arbeitsspeicher der Bitmap wiederverwendet wird, was zu einer verbesserten Leistung führt und sowohl die Arbeitsspeicherzuweisung als auch die Arbeitsspeicherzuweisung entfernt werden. Es gibt jedoch bestimmte Einschränkungen bei der Verwendung von inBitmap. Vor Android 4.4 (API-Level 19) werden nur Bitmaps gleicher Größe unterstützt. Weitere Informationen finden Sie in der Dokumentation zu inBitmap.

Bitmap zur späteren Verwendung speichern

Das folgende Snippet zeigt, wie eine vorhandene Bitmap für eine mögliche spätere Verwendung in der Beispiel-App gespeichert wird. Wenn eine App unter Android 3.0 oder höher ausgeführt wird und eine Bitmap aus LruCache entfernt wird, wird ein Softverweis auf die Bitmap zur späteren Wiederverwendung mit inBitmap in einem HashSet platziert:

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

Vorhandene Bitmap verwenden

In der laufenden Anwendung prüfen Decodermethoden, ob eine Bitmap vorhanden ist, die sie verwenden können. Beispiele:

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

Das nächste Snippet zeigt die Methode addInBitmapOptions(), die im Snippet oben aufgerufen wird. Dabei wird nach einer vorhandenen Bitmap gesucht, die als Wert für inBitmap festgelegt werden soll. Beachten Sie, dass mit dieser Methode nur dann ein Wert für inBitmap festgelegt wird, wenn eine passende Übereinstimmung gefunden wird. Ihr Code sollte niemals davon ausgehen, dass eine Übereinstimmung gefunden wird:

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

Schließlich bestimmt diese Methode, ob eine mögliche Bitmap die Größenkriterien erfüllt, die für inBitmap verwendet werden sollen:

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