1 / 6

Memory Management with Bitmaps

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.

Télécharger la présentation

Memory Management with Bitmaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • That’s 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. 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();

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

More Related