管理图片内存
——Android官网原文翻译
除了在图片缓存中描述的内容之外,还有一些可以促进GC工作和图片重用的事。推荐的策略依赖于您的目标Android版本。示例代码包含了一个类,向您展示怎样基于不同Android版本设计出高效的应用程序。
这里是Android的图片内存管理怎样逐步形成了,以便您为本节课做好准备:
1.在Android2.2(API Level8)甚至更低,当垃圾收集发生时,您的应用程序线程会停止。这会导致停止以降低应用程序的性能。Andorid2.3添加了并行的垃圾收集,这意味着一旦图片不在引用会被即可释放掉。
2.在Andorid2.3.3(API Level 10)或者更低,Bitamp的底层像素数据被保存在本地内存。它由Bitmap自己分配,而Bitmap保存在Dalvik虚机的堆上。本地内存中的像素数据并不会以一种可预期的方式释放掉,这潜在的导致应用程序会超出内存限制而崩溃。Android3.0(API Level 11)以后,像素数据也联通Bitmap实例一起被保存在了Dalvik虚机的堆上。
这一节将描述怎样为不同的Android版本优化Bitmap的内存管理。
在Android2.3.3或者更低版本管理内存
在Android2.3.3(API Level 10)和更低版本,使用recycle()是推荐的。如果您要在您的应用程序中显示大量的图片,您有可能会遇到OutOfMemoryErrror的错误。recycle()方法允许应用程序尽快的释放内存。
注意:您应该在您确定bitmap对象已经不在使用时调用recycle()方法。如果您调用了recycle()方法却又尝试绘制bitmap对象,您会遇到这样的错误:"Canvas:trying to use a recycled bitmap"。
下面的代码片段给出了调用recycle()的示例。它使用引用计数(使用mDisplayRefCount和mCacheRefCount两个变量)来判断bitmap对象目前是在显示还是在缓存中。当满足下面两个条件时代码会回收bitmap对象:
1.引用计数mDisplayRefCount和mCacheRefCount都为0。
2.Bitmap不为null,并且还未被回收。
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();
}
在Android3.0及以上版本管理内存
Andorid3.0(API Level 11)引入了BitmapFactory.Options.inBitmap字段。如果该选项被设置,使用了该options对象的解码方法在加载内容时会尝试重用已经存在的bitmap对象。这意味着bitmap对象的内存被重用了,这将导致性能的提升,并将移除已分配和解除分配的内存。这里有些使用inBitmap的说明:
1.被重用的Bitmap大小必须于数据源(确保相同大小的内存被重用)大小相同,并且格式必须是JPEG或者PNG(Resource或者Stream)。
2.被重用的bitmap的Bitmap.Config覆盖inPreferredConfig的设置。
3.你应该始终使用decode方法返回的bitmap对象,因为你不能假定图片确实重用成功了(如果图片大小不匹配的话)。
保存图片以备后用
以下的片段论述了一张已经存在的bitmap怎样保存使之以后有可能被重用。当应用程序在Android3.0或更高版本运行时,一个bitmap对象被从LruCache中移除,一个该图片的弱引用被设置于一个HashSet,为了之后有可能的重用。
HashSet<SoftReference<Bitmap>> mReusableBitmaps;
private LruCache<String, BitmapDrawable> mMemoryCache;
// If you're running on Honeycomb or newer, create
// a HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
mReusableBitmaps = 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()));
}
}
}
....
}
使用一张已存在的图片
在正在运行的应用程序中,decoder方法检查是否有可用的已存在的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);
}
下一个片段展示了上一个代码片段中调用的addInbitmapOptions()方法。它寻找已存在的bitmap作为inBitmap的值。注意这个方法如果找到了合适bitmap仅仅给inBitmap设置值(您的代码永远不要假定合适的bitmap一定会被找到):
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()) {
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;
}
最后,这个方法决定候选的bitmap的大小是否符合作为inBitmap的标准:
private static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
int width = targetOptions.outWidth / targetOptions.inSampleSize;
int height = targetOptions.outHeight / targetOptions.inSampleSize;
// Returns true if "candidate" can be used for inBitmap re-use with
// "targetOptions".
return candidate.getWidth() == width && candidate.getHeight() == height;
}