Managing Bitmap Memory管理Bitmap記憶體 In addition to the steps described in Caching Bitmaps, there are specific things you can do to facilitate garbage collection and bitmap reuse. The recommended strategy depends on which version(s) of Android you are targeting. The BitmapFun sample app included with this class shows you how to design your app to work efficiently across different versions of Android.除了在Caching Bitmaps裡描述的,還有一些具體的事情有助於記憶體回收和bitmap的重用建議的策略取決於你針對的Android版本。BitmapFun樣本應用中包括這個類,它展示給你如何設計你的應用使得跨版本工作更有效率 To set the stage for this lesson, here is how Android's management of bitmap memory has evolved:為課程打基礎,下面是android的bitmap記憶體管理是如何演化的 On Android Android 2.2 (API level 8) and lower, when garbage collection occurs, your app's threads get stopped. This causes a lag that can degrade performance. Android 2.3 adds concurrent garbage collection, which means that the memory is reclaimed soon after a bitmap is no longer referenced.在Android2.2(API 8)以及更低的版本中,當發生記憶體回收時,你的應用線程會停止。這會導致延遲,使得效能降低Android2.3添加了並發垃圾收集,這意為著一個bitmap不再被引用的時候,記憶體很快就被回收 On Android 2.3.3 (API level 10) and lower, the backing pixel data for a bitmap is stored in native memory. It is separate from the bitmap itself, which is stored in the Dalvik heap. The pixel data in native memory is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash. As of Android 3.0 (API Level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap.在Android2.3.3(API 10)和更低的版本中,bitmap的像素資料是儲存在native記憶體中的它獨立於bitmap本身,bitmap是儲存在Dalvik堆中的在native記憶體中的bitmap的像素資料不會在一個可預測的行為之釋放,潛在的導致應用記憶體超過限制並且崩潰在android3.0(API 11)中,bitmap的像素資料存放區在Dalvik堆中於bitmap相關聯 The following sections describe how to optimize bitmap memory management for different Android versions.下面章節講述在不同的android版本中,如何最佳化bitmap記憶體管理 Manage Memory on Android 2.3.3 and Lower在android2.3.3以及更低的版本中管理記憶體 On Android 2.3.3 (API level 10) and lower, using recycle() is recommended. If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible.在android2.3.3以及更低的版本中,建議使用recycle()如果你在你的應用中大量的顯示bitmap資料,你很可能得到OutOfMemoryError錯誤recycle()方法允許一個應用儘快回收記憶體 Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap".注意:僅當你確定這個bitmap不會再被使用的時候,你才應該使用recycle()如果你調用了recycle(),之後又試圖繪製這個bitmap,你會得到 錯誤:“Canvas: trying to use a recycled bitmap” The following code snippet gives an example of calling recycle(). It uses reference counting (in the variables mDisplayRefCount and mCacheRefCount) to track whether a bitmap is currently being displayed or in the cache. The code recycles the bitmap when these conditions are met:下面的代碼片斷給出了一個調用recycle()例子它使用引用計數(在變數mDisplayRefCount 和 mCacheRefCount中)來根中一個bitmap當前正在被顯示還是在緩衝中代碼回收bitmap需要的條件是: The reference count for both mDisplayRefCount and mCacheRefCount is 0.The bitmap is not null, and it hasn't been recycled yet.引用計數mDisplayRefCount和mCacheRefCount都要=0bitmap不為null,並且它還沒有被回收[java] 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(); } Manage Memory on Android 3.0 and Higher在android3.0以及更高的版本中管理記憶體 Android 3.0 (API Level 11) introduces the BitmapFactory.Options.inBitmap field. If this option is set, decode methods that take the Options object will attempt to reuse an existing bitmap when loading content. This means that the bitmap's memory is reused, resulting in improved performance, and removing both memory allocation and de-allocation. There are some caveats in using inBitmap:Android3.0(API 11)引入了BitmapFactory.Options.inBitmap如果設定了這個選項,當載入內容的時候,使用Options對象的解碼方法將會嘗試複用一個存在的bitmap這意為著bitmap的記憶體被複用了,導致效能提升,並且無須分配與重新分配記憶體使用量inBitmap有一些需要注意的地方: The reused bitmap must be of the same size as the source content (to make sure that the same amount of memory is used), and in JPEG or PNG format (whether as a resource or as a stream).重用的bitmap必須與源內容尺寸一致(保證相同的記憶體被使用),並且是JPEG或者PNG格式(作為resource或者stream) The configuration of the reused bitmap overrides the setting of inPreferredConfig, if set.You should always use the returned bitmap of the decode method, because you can't assume that reusing the bitmap worked (for example, if there is a size mismatch).Save a bitmap for later use如果設定了inPreferredConfig,重用的bitmap的配置就重寫了inPreferredConfig你應該總數使用解碼函數返回的bitmap,因為你不能假定重用的bitmap能工作(例如,如果尺寸不匹配)為之後使用儲存一個bitmap The following snippet demonstrates how an existing bitmap is stored for possible later use in the sample app. When an app is running on Android 3.0 or higher and a bitmap is evicted from the LruCache, a soft reference to the bitmap is placed in a HashSet, for possible reuse later with inBitmap:下面的代碼片斷示範在樣本應用中,一個存在的圖片如何為之後可能的使用而儲存當一個應用運行在andoid3.0或者更高版本中,並且LruCache已經回收了bitmap,為了之後可能的使用inBitmap重用,bitmap的一個軟引用在HashSet中存放。[java] 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())); } } } .... } Use an existing bitmap使用已經存在的bitmapIn the running app, decoder methods check to see if there is an existing bitmap they can use. For example:在運行中的應用中,解碼方法檢查是否有已經存在的bitmap可供使用[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); } The next snippet shows the addInBitmapOptions() method that is called in the above snippet. It looks for an existing bitmap to set as the value for inBitmap. Note that this method only sets a value for inBitmap if it finds a suitable match (your code should never assume that a match will be found):下面的代碼片斷展示上面代碼中調用的addInBitmapOptions()方法它尋找一個存在的bitmap作為inBitmap的值注意,這個方法只設定inBitmap,如果找到了合適的匹配的話(你的代碼不該假設這個匹配一定會找到)[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 (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; } Finally, this method determines whether a candidate bitmap satisfies the size criteria to be used for inBitmap:最後,這個方法決定一個備選bitmap是否滿足設定inBitmap的尺寸標準[java] 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; }