Managing Bitmap Memory
来源:互联网 发布:淘宝有人工客服电话嘛? 编辑:程序博客网 时间:2024/06/08 18:49
TRY IT OUT
BitmapFun.zip
In addition to {除...之外} the steps described in Caching Bitmaps, there are specific things you can do to facilitate {[fə'sɪlɪteɪt] 帮助,使...更容易} 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.
To set the stage {[steɪdʒ] 步骤} for this lesson, here is how Android's management of bitmap memory has evolved {[ɪ'vɒlvd] 进化}:
- On Android Android 2.2 (API level 8) and lower, when garbage collection occurs {[ə'kɜːr] 发生}, your app's threads get stopped. This causes a lag {[læɡ] 降低,时间间隔} that can degrade {[dɪ'ɡreɪd] 降低} performance {[pər'fɔːrməns] 性能}. Android 2.3 adds concurrent garbage collection, which means that the memory is reclaimed {[rɪ'kleɪm] 回收} soon after a bitmap is no longer referenced.
- 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 {[pə'tenʃəli] 潜在地} causing an application to briefly {['briːfli] 简单地} 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.
The following sections describe how to optimize bitmap memory management for different Android versions.
Manage Memory on Android 2.3.3 and Lower
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.
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"
.
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:
- The reference count for both
mDisplayRefCount
andmCacheRefCount
is 0. - The bitmap is not
null
, and it hasn't been recycled yet.
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
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 {['kæviæt] 防止错误的说明} in using 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).
- The
configuration
of the reused bitmap overrides the setting ofinPreferredConfig
, if set. - You should always use the returned bitmap of the decode method, because you can't assume {[ə'suːm] 假定} that reusing the bitmap worked (for example, if there is a size mismatch).
Save a bitmap for later use
The following snippet {['snɪpɪt] 片段} 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 {[ɪ'vɪkt] 已发回收} from the
LruCache
, a soft reference to the bitmap is placed in aHashSet
, for possible reuse later withinBitmap
: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
In the running app, decoder methods check to see if there is an existing bitmap they can use. For example:
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 theaddInBitmapOptions()
method that is called in the above snippet. It looks for an existing bitmap to set as the value forinBitmap
. Note that this method only sets a value forinBitmap
if it finds a suitable match (your code should never assume that a match will be found):private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) { // inBitmap only works with mutable {['mjuːtəbl] 可变的} 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
: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;}
- Managing Bitmap Memory
- Managing Bitmap Memory
- Managing Bitmap Memory
- Managing Bitmap Memory
- Managing Bitmap Memory [管理Bitmap内存]
- Managing Bitmap Memory(管理位图内存)
- Managing Bitmap Memory(管理位图内存)
- Displaying Bitmaps Efficiently之Managing Bitmap Memory
- Android Managing Bitmap Memory And Loading Large Bitmaps Efficiently
- Managing Bitmap Memory管理bitmap内存(Android官方文档翻译——四)
- Oracle study: Managing Memory
- Managing Your App's Memory
- Managing Your App's Memory
- Managing Your App's Memory
- ZT:Managing Heap Memory in Win32
- Oracle Managing Memory - AMM与ASMM
- Managing Your App's Memory 翻译
- Managing Your App's Memory(8)篇文章
- 给新手程序员和学生的书籍
- 格雷码
- 《集体智慧编程》第九章 关于婚介数据集的SVM分类
- dll动态加载的方式(转)
- 一个java程序员的生活<二> ----程序员第一份工作
- Managing Bitmap Memory
- 集合的子集 (其实是组合问题)
- gnu make(2)
- Data Structures and Algorithm Analysis in C (数据结构与算法分析) 读书总结
- Vmware中装Linux Mint 15 添加 1600x900分辨率
- Windows 7系统安装MySQL5.5.21图解
- 【Button】按钮onTouch事件监听
- 查看3g智能手机网页源代码
- GNU Make 使用手册(中译版)翻译:于凤昌