Bitmaps加载之内存管理

除了前面说的Bitmap缓存之外,还有一些事情我们可以做来使用好GC和Bitmap的重用. 对于不同的Android版本要做不同的处理,这样才能达到高效使用Bitmap的效果,这是推荐的策略.

这里先介绍一些关于Android中Bitmap内存管理的基础知识铺垫一下:

  • 在Android2.2(API 8)及以前,当GC开始回收时,app的所有线程都将停止, 这就导致了延迟的产生,进而影响体验. Android2.3及之后就不会用这个问题了,因此增加了GC的并发处理,也就意味着Bitmap被清理之后app的可用空间会很快回收回来.
  • Android2.3.3(API 10)及以前,Bitmap的图片数据是保存在native memory上, 而Bitmap对象是保存在Dalvik的heap上,这样这两个就分离开了,就会导致内存释放不及时从而带来潜在的OOM. 在Android 3.0(API 11)之后这个问题就解决了,因为这两个都被放在Dalvik的heap上.

下面介绍如何根据不同的Android版本来管理Bitmap的内存.

Android2.3.3及以下

在这个版本范围内,推荐使用recycle()方法, 该方法会尽快把Bitmap的内存回收回来.

  • 注意: 你只有在确定这个Bitmap不再使用的情况下才去调用recycle()方法. 不然如果你调用recycle()之后又要想去使用之前的Bitmap,会抛出一个异常:"Canvas: trying to use a recycled bitmap"

下面的代码是Demo中RecyclingBitmapDrawable的一部分,其中mDisplayRefCount和mCacheRefCount这两个变量用来记录该Bitmap显示和缓存情况,具体回收条件如下:

  1. mDisplayRefCount和mCacheRefCount的值都为0.
  2. Bitmap不为空.

完整代码请参考官方demo,下面Reference带下载地址.

private int mCacheRefCount = 0;
private int mDisplayRefCount = 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) {
            mDisplayRefCount++;
            mHasBeenDisplayed = true;
        } else {
            mDisplayRefCount--;
        }
    }
    // 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) {
            mCacheRefCount++;
        } else {
            mCacheRefCount--;
        }
    }
    // 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 (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed
            && 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属性.如果设置了该属性, BitmapFactory带有Options参数的decode相关方法会尝试去重用已存在的Bitmap, 这就意味这Bitmap的内存空间得到了重用, 就可以改善性能,减少内存分配和回收.
但是使用inBitmap这个属性有一些限制, 有一点比较特别的是在Android4.4以前(API 19),只有相同大小的Bitmap才可以重用,具体可以看inBitmap文档.

下面看具体实例:

1. 保存Bitmap

下面是用一个HashSet来保存从LruCache中移除的Bitmap的软引用.

Set<SoftReference<Bitmap>> mReusableBitmaps;
private LruCache<String, BitmapDrawable> mMemoryCache;

// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    mReusableBitmaps =
            Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
}

mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.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.
                mReusableBitmaps.add
                        (new SoftReference<Bitmap>(oldValue.getBitmap()));
            }
        }
    }
....
}

2. 重用Bitmap

查找是否有可重用的Bitmap

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

如果找到可用的就设置inBitmap

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 (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
        synchronized (mReusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator
                    = mReusableBitmaps.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;
}

查找时具体的匹配条件如下

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

Reference

  1. Managing Bitmap Memory
  2. 官方DisplayingBitmaps Demo
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,687评论 25 708
  • HereAndroid的内存优化是性能优化中很重要的一部分,而避免OOM又是内存优化中比较核心的一点。这是一篇关于...
    HarryXR阅读 3,857评论 1 24
  • 本文转载来源 http://www.csdn.net/article/2015-09-18/2825737/1 (...
    yoosir阅读 1,143评论 0 5
  • 今天从超市买来几棵虎皮兰。第一次主动尝试养花。把以前死掉的花除去,重新灌土,浇水。几个大的虎尾兰就种好了。放...
    刘琦_8bf2阅读 411评论 0 0
  • 一个阴雨天,某医院肾病科里来了一位年轻的小伙子,20多岁的年龄,但脸色晦暗,疲倦不堪,一家人全都神色凝重,说小伙子...
    linjingxia阅读 309评论 0 0