Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Memory Management with Bitmaps How to avoid bitmap size exceeds VM budget."— Presentation transcript:

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

2 Java/Android Constraints Application VM is capped at 16/24Mbytes –Thats for everything Garbage Collection is Asynchronous – System.gc() is only a suggestion

3 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)

4 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 title int width = display.getWidth(); int height = display.getHeight() - mPuzzleView.GetOffsetY(); int minImageSize = Math.max( width, height ); // Decode image size BitmapFactory.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 inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; b = BitmapFactory.decodeStream( is, null, o2 ); return b; }

5 Whats 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();

6 Where and When onDestroy() onPause()/onResume() –Can adversely affect user experience –Can fragment memory


Download ppt "Memory Management with Bitmaps How to avoid bitmap size exceeds VM budget."

Similar presentations


Ads by Google