60 likes | 198 Views
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.
E N D
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 • 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)
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; • }
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();
Where and When • onDestroy() • onPause()/onResume() • Can adversely affect user experience • Can fragment memory