memory management with bitmaps

6
Memory Management with Bitmaps How to avoid bitmap size exceeds VM budget

Upload: michaela-sweetman

Post on 30-Dec-2015

31 views

Category:

Documents


0 download

DESCRIPTION

Memory Management with Bitmaps. How to avoid bitmap size exceeds VM budget. Java/Android Constraints. Application VM is capped at 16/24Mbytes That’s for everything Garbage Collection is Asynchronous System.gc() is only a suggestion. Size Does Matter. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Memory Management with Bitmaps

Memory Management with Bitmaps

How to avoid bitmap size exceeds VM budget

Page 2: Memory Management with Bitmaps

Java/Android Constraints

• Application VM is capped at 16/24Mbytes– That’s for everything

• Garbage Collection is Asynchronous– System.gc() is only a suggestion

Page 3: Memory Management with Bitmaps

Size Does Matter

• The size of the image in the filesystem is irrelevant

• How to calculate the in memory size– H x W x Depth– 480 x 860 x 8 = 412.6KBytes– 480 x 860 x 32 = 1.6MByes (ARGB_8888)

Page 4: Memory Management with Bitmaps

What is a Programmer To Do?• Adjust the Sample Size

private Bitmap decodeFile( InputStream is ){

Bitmap b = null;Display display = getWindowManager().getDefaultDisplay();// Allow for the titleint width = display.getWidth();int height = display.getHeight() - mPuzzleView.GetOffsetY();int minImageSize = Math.max( width, height );// Decode image sizeBitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeStream( is, null, options );int scale = 1;if( options.outHeight > minImageSize || options.outWidth > minImageSize ){ scale = (int)Math.pow( 2, (int)Math.round( Math.log( minImageSize / (double)Math.max( options.outHeight, options.outWidth ) ) / Math.log( 0.5 ) ) );}// Decode with inSampleSizeBitmapFactory.Options o2 = new BitmapFactory.Options();o2.inSampleSize = scale;b = BitmapFactory.decodeStream( is, null, o2 );return b;

}

Page 5: Memory Management with Bitmaps

What’s a Programmer To Do?

• Clean up after yourself

for( Tile tile : mTilesList )

{if( tile != null && tile.mBitmap != null )

{

tile.mBitmap.recycle();

tile.mBitmap = null;

}

}

System.gc();

Page 6: Memory Management with Bitmaps

Where and When

• onDestroy()

• onPause()/onResume()– Can adversely affect user experience– Can fragment memory