zoukankan      html  css  js  c++  java
  • bitmap size exceeds VM budget

    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.

  • 相关阅读:
    javascript阻止子元素继承父元素事件
    UTC 时间转化为北京时间
    uniapp中引入less文件
    HDU 1002 A + B Problem II(大数据)
    FatMouse's Speed(dp)
    Monkey and Banana(dp)
    Piggy-Bank(dp,背包)
    Longest Ordered Subsequence(最长上升子序列,dp)
    我的第一篇博客
    redis优化方案
  • 原文地址:https://www.cnblogs.com/xitang/p/2599479.html
Copyright © 2011-2022 走看看