Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drowning in Images — Memory Management

Drowning in Images — Memory Management

Outlines best-practices and problems encountered managing bitmaps in an Android application

Dallas Gutauckis

April 24, 2012
Tweet

More Decks by Dallas Gutauckis

Other Decks in Programming

Transcript

  1. github.com/dallasgutauckis §  Use  a  download  manager   §  Don't  allow

     mulRple  concurrent  downloads  of  the  same  image   §  Don't  download  on  the  UI  thread   §  Lists  (GridView,  ListView,  AdapterView)   §  Avoid  downloading  images  while  flinging  (use  ScrollListener)   §  Cache!   §  Bitmaps  stored  naRvely  vs.  on  applicaRon  heap   §  LruCache<String,  Bitmap>  (Least-­‐recently  used)   §  Override  sizeOf(String,  Bitmap)   §  Override  entryRemoved(boolean,  K,  V,  V)   §  OutOfMemoryError   §  Evict!  Recycle!  Resize!   14  
  2. github.com/dallasgutauckis §  RESAMPLE   §  Save  memory  when  determining  resample

     factor,  just  decode  bounds     §  inSampleSize  to  tell  the  system  to  resample   §  Number  is  a  raRo  (1  pixel  for  every  X  original  pixels)   §  Factors  of  2  are  typically  more  performant   15  
  3. github.com/dallasgutauckis §  Avoid  the  ominous  "bitmap  is  recycled"  excepRon  

    §  Check  for  recycled  bitmaps   §  Bitmap#isRecycled();   §  ImageView#setImageBitmap(null);   §  Re-­‐download   16   RuntimeException: trying to use a recycled bitmap android.graphics.Bitmap
  4. github.com/dallasgutauckis §  Trying  to  draw  recycled  bitmaps   §  Determining

     the  size  of  a  Bitmap  by  decoding  the  enRre  Bitmap   §  Downloading  items  that  may  never  be  visible   §  LruCache  is  thread-­‐safe.  Synchronize!   17  
  5. github.com/dallasgutauckis §  I/O  Memory  Management  Talk   h>p://www.youtube.com/watch?v=_CruQY55HOk   § 

    Displaying  Bitmaps  Efficiently   h>p://developer.android.com/training/displaying-­‐bitmaps/index.html   18