喜糖

移动开发工程师 。涉及 android、ios、jni

导航

bitmap size exceeds VM budget

Posted on 2013-08-28 15:25  喜糖  阅读(282)  评论(0编辑  收藏  举报

bitmap size exceeds VM budget

we can avoid this error by the following parts:
1  its not how much images the screen has, but being carefull on cleaning everything up when finishing the activity 
2   Technique to Avoid, #3: Going Overboard with Layouts:
Due to changes in the View rendering infrastructure, unreasonably deep (more than 10 or so) or broad (more than 30 total) View hierarchies in layouts are now likely to cause crashes. This was always a risk for excessively complex layouts, but you can think of Android 1.5 as being better than 1.1 at exposing this problem. Most developers won't need to worry about this, but if your app has very complicated layouts, you'll need to put it on a diet. You can simplify your layouts using the more advanced layout classes like FrameLayout and TableLayout. 

If your application  involve many images,(some times may be seldom.),if causing this error,you can check the following aspects:  

   1 .   Is your "Bitmap" object  released before the "Activity"  finished which it's belonged ?
         you can use "recycle()","System.gc()", if a  arraylist,you can use "clear()"  ,do it  before your activity finished.
         for example:
             

try{                  
      Intent myintent=new Intent();
      Bundle bun=new Bundle();
      bun.putInt("position", position);
      myintent.putExtras(bun);
      setResult(RESULT_OK,myintent);          
      imageAdapter.bitmaplist.clear();
      System.gc();
      thisActivity.finish();
      }
      catch (OutOfMemoryError e) {
          e.printStackTrace();
      }

 


    2. You can consider to reduce the  qulity of the image .Although is not a good idea.
       

BitmapFactory.Options opts=new BitmapFactory.Options();
                    opts.inSampleSize =2; //the value you can set bigger than 1.
                    Bitmap imgBitmap=null;
                    imgBitmap = BitmapFactory.decodeStream(fs, null, opts);

    3. Make pictures smaller.when you generate the picture ,you can do :
  

Bitmap bitmap = Bitmap.createScaledBitmap(cacheBitmap,330, 300, false);   


    4. Definition your software' memory size,we can use this class: dalvik.system.VMRuntime
 

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;

    when using:

VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //here,set 6MB

    5. make the Dalvik virtual machine optimized on heap memory allocation.

private final static float TARGET_HEAP_UTILIZATION = 0.75f;

    when using:

VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);


    6. Don't forget.. make the debug more convenient  
   

try {
    
        ……
    
    } catch (OutOfMemoryError e) {
    
        e.printStackTrace();
    
    }

    
Ok ,that's all.