비트맵 메모리 관리

참고: 대부분의 경우 Glide 라이브러리를 사용하여 앱에서 비트맵을 가져오고 디코딩하고 표시하는 것을 추천합니다. Glide는 이러한 작업을 비롯하여 Android에서 비트맵과 기타 이미지를 사용하는 다른 관련 작업을 처리할 때 대부분의 복잡성을 추상화합니다. Glide 사용 및 다운로드에 관한 자세한 내용은 GitHub의 Glide 저장소를 참고하세요.

비트맵 캐싱에 설명한 단계 외에도 가비지 컬렉션 및 비트맵 재사용을 쉽게 하기 위한 몇 가지 특별한 것이 있습니다. 타겟팅하는 Android의 버전에 따라 추천하는 전략이 다릅니다. 이 클래스와 함께 포함된 BitmapFun 샘플 앱은 여러 버전의 Android에서 효율적으로 작동하도록 앱을 설계하는 방법을 보여줍니다.

이 과정을 이해하는 데 도움이 되는 아래 내용을 참조하여 Android의 비트맵 메모리 관리 방식이 어떻게 발전했는지 알아보세요.

  • 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) 이상에서는 비트맵 픽셀 데이터가 네이티브 힙에 저장됩니다.

다음 섹션에서는 여러 버전의 Android에서 비트맵 메모리 관리를 최적화하는 방법을 설명합니다.

Android 2.3.3 이하에서 메모리 관리

Android 2.3.3(API 수준 10) 이하에서는 recycle()을 사용하는 것이 좋습니다. 앱에서 대량의 비트맵 데이터를 표시하면 OutOfMemoryError 오류가 발생할 수 있습니다. recycle() 메서드를 사용하면 앱에서 최대한 빨리 메모리를 회수할 수 있습니다.

주의: 확실히 비트맵을 더 이상 사용하지 않을 때만 recycle()을 사용해야 합니다. recycle()을 호출한 후에 비트맵을 그리려고 시도하면 "Canvas: trying to use a recycled bitmap" 오류가 발생합니다.

다음 코드 스니펫은 recycle()을 호출하는 예를 보여줍니다. 이 코드 스니펫은 변수 mDisplayRefCountmCacheRefCount에서 참조 횟수 계산을 사용하여 비트맵이 현재 표시되고 있는지 아니면 캐시에 있는지 추적합니다. 아래의 조건이 충족되면 코드가 비트맵을 재활용합니다.

  • mDisplayRefCountmCacheRefCount의 참조 횟수가 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 문서를 참고하세요.

나중에 사용하기 위해 비트맵 저장

다음 스니펫은 나중에 샘플 앱에서 사용할 수 있도록 기존 비트맵을 저장하는 방법을 보여줍니다. 앱이 Android 3.0 이상에서 실행 중일 때 비트맵이 LruCache에서 제거될 때 비트맵에 관한 소프트 참조는 나중에 inBitmap에서 재사용할 수 있도록 HashSet에 배치됩니다.

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

자바

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