memory management with bitmaps
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 PresentationTRANSCRIPT
![Page 1: Memory Management with Bitmaps](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/1.jpg)
Memory Management with Bitmaps
How to avoid bitmap size exceeds VM budget
![Page 2: Memory Management with Bitmaps](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071807/56812b1a550346895d8f0c0f/html5/thumbnails/6.jpg)
Where and When
• onDestroy()
• onPause()/onResume()– Can adversely affect user experience– Can fragment memory