1page.title=Managing Bitmap Memory 2parent.title=Displaying Bitmaps Efficiently 3parent.link=index.html 4 5trainingnavtop=true 6 7@jd:body 8 9<div id="tb-wrapper"> 10<div id="tb"> 11 12<h2>This lesson teaches you to</h2> 13<ol> 14 <li><a href="#recycle">Manage Memory on Android 2.3.3 and Lower</a></li> 15 <li><a href="#inBitmap">Manage Memory on Android 3.0 and Higher</a></li> 16</ol> 17 18<h2>You should also read</h2> 19<ul> 20 <li><a href="http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html">Memory Analysis for Android Applications</a> blog post</li> 21 <li><a href="http://www.google.com/events/io/2011/sessions/memory-management-for-android-apps.html">Memory management for Android Apps</a> Google I/O presentation</li> 22 <li><a href="{@docRoot}design/patterns/swipe-views.html">Android Design: Swipe Views</a></li> 23 <li><a href="{@docRoot}design/building-blocks/grid-lists.html">Android Design: Grid Lists</a></li> 24</ul> 25 26<h2>Try it out</h2> 27 28<div class="download-box"> 29 <a href="{@docRoot}downloads/samples/DisplayingBitmaps.zip" class="button">Download the sample</a> 30 <p class="filename">DisplayingBitmaps.zip</p> 31</div> 32 33</div> 34</div> 35 36<p>In addition to the steps described in <a href="cache-bitmap.html">Caching Bitmaps</a>, 37there are specific things you can do to facilitate garbage collection 38and bitmap reuse. The recommended strategy depends on which version(s) 39of Android you are targeting. The {@code BitmapFun} sample app included with 40this class shows you how to design your app to work efficiently across 41different versions of Android.</p> 42 43<p>To set the stage for this lesson, here is how Android's management of 44bitmap memory has evolved:</p> 45<ul> 46 <li> 47On Android Android 2.2 (API level 8) and lower, when garbage 48collection occurs, your app's threads get stopped. This causes a lag that 49can degrade performance. 50<strong>Android 2.3 adds concurrent garbage collection, which means that 51the memory is reclaimed soon after a bitmap is no longer referenced.</strong> 52</li> 53 54 <li>On Android 2.3.3 (API level 10) and lower, the backing pixel data for a 55bitmap is stored in native memory. It is separate from the bitmap itself, 56which is stored in the Dalvik heap. The pixel data in native memory is 57not released in a predictable manner, potentially causing an application 58to briefly exceed its memory limits and crash. 59<strong>As of Android 3.0 (API level 11), the pixel data is stored on the 60Dalvik heap along with the associated bitmap.</strong></li> 61 62</ul> 63 64<p>The following sections describe how to optimize bitmap memory 65management for different Android versions.</p> 66 67<h2 id="recycle">Manage Memory on Android 2.3.3 and Lower</h2> 68 69<p>On Android 2.3.3 (API level 10) and lower, using 70{@link android.graphics.Bitmap#recycle recycle()} 71is recommended. If you're displaying large amounts of bitmap data in your app, 72you're likely to run into 73{@link java.lang.OutOfMemoryError} errors. The 74{@link android.graphics.Bitmap#recycle recycle()} method allows an app 75to reclaim memory as soon as possible.</p> 76 77<p class="note"><strong>Caution:</strong> You should use 78{@link android.graphics.Bitmap#recycle recycle()} only when you are sure that the 79bitmap is no longer being used. If you call {@link android.graphics.Bitmap#recycle recycle()} 80and later attempt to draw the bitmap, you will get the error: 81{@code "Canvas: trying to use a recycled bitmap"}.</p> 82 83<p>The following code snippet gives an example of calling 84{@link android.graphics.Bitmap#recycle recycle()}. It uses reference counting 85(in the variables {@code mDisplayRefCount} and {@code mCacheRefCount}) to track 86whether a bitmap is currently being displayed or in the cache. The 87code recycles the bitmap when these conditions are met:</p> 88 89<ul> 90<li>The reference count for both {@code mDisplayRefCount} and 91{@code mCacheRefCount} is 0.</li> 92<li>The bitmap is not {@code null}, and it hasn't been recycled yet.</li> 93</ul> 94 95<pre>private int mCacheRefCount = 0; 96private int mDisplayRefCount = 0; 97... 98// Notify the drawable that the displayed state has changed. 99// Keep a count to determine when the drawable is no longer displayed. 100public void setIsDisplayed(boolean isDisplayed) { 101 synchronized (this) { 102 if (isDisplayed) { 103 mDisplayRefCount++; 104 mHasBeenDisplayed = true; 105 } else { 106 mDisplayRefCount--; 107 } 108 } 109 // Check to see if recycle() can be called. 110 checkState(); 111} 112 113// Notify the drawable that the cache state has changed. 114// Keep a count to determine when the drawable is no longer being cached. 115public void setIsCached(boolean isCached) { 116 synchronized (this) { 117 if (isCached) { 118 mCacheRefCount++; 119 } else { 120 mCacheRefCount--; 121 } 122 } 123 // Check to see if recycle() can be called. 124 checkState(); 125} 126 127private synchronized void checkState() { 128 // If the drawable cache and display ref counts = 0, and this drawable 129 // has been displayed, then recycle. 130 if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed 131 && hasValidBitmap()) { 132 getBitmap().recycle(); 133 } 134} 135 136private synchronized boolean hasValidBitmap() { 137 Bitmap bitmap = getBitmap(); 138 return bitmap != null && !bitmap.isRecycled(); 139}</pre> 140 141<h2 id="inBitmap">Manage Memory on Android 3.0 and Higher</h2> 142 143<p>Android 3.0 (API level 11) introduces the 144{@link android.graphics.BitmapFactory.Options#inBitmap BitmapFactory.Options.inBitmap} 145field. If this option is set, decode methods that take the 146{@link android.graphics.BitmapFactory.Options Options} object 147will attempt to reuse an existing bitmap when loading content. This means 148that the bitmap's memory is reused, resulting in improved performance, and 149removing both memory allocation and de-allocation. However, there are certain restrictions with how 150{@link android.graphics.BitmapFactory.Options#inBitmap} can be used. In particular, before Android 1514.4 (API level 19), only equal sized bitmaps are supported. For details, please see the 152{@link android.graphics.BitmapFactory.Options#inBitmap} documentation. 153 154<h3>Save a bitmap for later use</h3> 155 156<p>The following snippet demonstrates how an existing bitmap is stored for possible 157later use in the sample app. When an app is running on Android 3.0 or higher and 158a bitmap is evicted from the {@link android.util.LruCache}, 159a soft reference to the bitmap is placed 160in a {@link java.util.HashSet}, for possible reuse later with 161{@link android.graphics.BitmapFactory.Options#inBitmap}: 162 163<pre>Set<SoftReference<Bitmap>> mReusableBitmaps; 164private LruCache<String, BitmapDrawable> mMemoryCache; 165 166// If you're running on Honeycomb or newer, create a 167// synchronized HashSet of references to reusable bitmaps. 168if (Utils.hasHoneycomb()) { 169 mReusableBitmaps = 170 Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>()); 171} 172 173mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) { 174 175 // Notify the removed entry that is no longer being cached. 176 @Override 177 protected void entryRemoved(boolean evicted, String key, 178 BitmapDrawable oldValue, BitmapDrawable newValue) { 179 if (RecyclingBitmapDrawable.class.isInstance(oldValue)) { 180 // The removed entry is a recycling drawable, so notify it 181 // that it has been removed from the memory cache. 182 ((RecyclingBitmapDrawable) oldValue).setIsCached(false); 183 } else { 184 // The removed entry is a standard BitmapDrawable. 185 if (Utils.hasHoneycomb()) { 186 // We're running on Honeycomb or later, so add the bitmap 187 // to a SoftReference set for possible use with inBitmap later. 188 mReusableBitmaps.add 189 (new SoftReference<Bitmap>(oldValue.getBitmap())); 190 } 191 } 192 } 193.... 194}</pre> 195 196 197<h3>Use an existing bitmap</h3> 198<p>In the running app, decoder methods check to see if there is an existing 199bitmap they can use. For example:</p> 200 201<pre>public static Bitmap decodeSampledBitmapFromFile(String filename, 202 int reqWidth, int reqHeight, ImageCache cache) { 203 204 final BitmapFactory.Options options = new BitmapFactory.Options(); 205 ... 206 BitmapFactory.decodeFile(filename, options); 207 ... 208 209 // If we're running on Honeycomb or newer, try to use inBitmap. 210 if (Utils.hasHoneycomb()) { 211 addInBitmapOptions(options, cache); 212 } 213 ... 214 return BitmapFactory.decodeFile(filename, options); 215}</pre 216 217<p>The next snippet shows the {@code addInBitmapOptions()} method that is called in the 218above snippet. It looks for an existing bitmap to set as the value for 219{@link android.graphics.BitmapFactory.Options#inBitmap}. Note that this 220method only sets a value for {@link android.graphics.BitmapFactory.Options#inBitmap} 221if it finds a suitable match (your code should never assume that a match will be found):</p> 222 223<pre>private static void addInBitmapOptions(BitmapFactory.Options options, 224 ImageCache cache) { 225 // inBitmap only works with mutable bitmaps, so force the decoder to 226 // return mutable bitmaps. 227 options.inMutable = true; 228 229 if (cache != null) { 230 // Try to find a bitmap to use for inBitmap. 231 Bitmap inBitmap = cache.getBitmapFromReusableSet(options); 232 233 if (inBitmap != null) { 234 // If a suitable bitmap has been found, set it as the value of 235 // inBitmap. 236 options.inBitmap = inBitmap; 237 } 238 } 239} 240 241// This method iterates through the reusable bitmaps, looking for one 242// to use for inBitmap: 243protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) { 244 Bitmap bitmap = null; 245 246 if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) { 247 synchronized (mReusableBitmaps) { 248 final Iterator<SoftReference<Bitmap>> iterator 249 = mReusableBitmaps.iterator(); 250 Bitmap item; 251 252 while (iterator.hasNext()) { 253 item = iterator.next().get(); 254 255 if (null != item && item.isMutable()) { 256 // Check to see it the item can be used for inBitmap. 257 if (canUseForInBitmap(item, options)) { 258 bitmap = item; 259 260 // Remove from reusable set so it can't be used again. 261 iterator.remove(); 262 break; 263 } 264 } else { 265 // Remove from the set if the reference has been cleared. 266 iterator.remove(); 267 } 268 } 269 } 270 } 271 return bitmap; 272}</pre> 273 274<p>Finally, this method determines whether a candidate bitmap 275satisfies the size criteria to be used for 276{@link android.graphics.BitmapFactory.Options#inBitmap}:</p> 277 278<pre>static boolean canUseForInBitmap( 279 Bitmap candidate, BitmapFactory.Options targetOptions) { 280 281 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 282 // From Android 4.4 (KitKat) onward we can re-use if the byte size of 283 // the new bitmap is smaller than the reusable bitmap candidate 284 // allocation byte count. 285 int width = targetOptions.outWidth / targetOptions.inSampleSize; 286 int height = targetOptions.outHeight / targetOptions.inSampleSize; 287 int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); 288 return byteCount <= candidate.getAllocationByteCount(); 289 } 290 291 // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1 292 return candidate.getWidth() == targetOptions.outWidth 293 && candidate.getHeight() == targetOptions.outHeight 294 && targetOptions.inSampleSize == 1; 295} 296 297/** 298 * A helper function to return the byte usage per pixel of a bitmap based on its configuration. 299 */ 300static int getBytesPerPixel(Config config) { 301 if (config == Config.ARGB_8888) { 302 return 4; 303 } else if (config == Config.RGB_565) { 304 return 2; 305 } else if (config == Config.ARGB_4444) { 306 return 2; 307 } else if (config == Config.ALPHA_8) { 308 return 1; 309 } 310 return 1; 311}</pre> 312 313</body> 314</html> 315