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.

  • 相关阅读:
    SparseArray<E>详解
    Android项目实战(十二):解决OOM的一种偷懒又有效的办法
    浅谈GridLayout(网格布局)
    Android项目实战(十一):moveTaskToBack(boolean ) 方法的使用
    Android Studio 项目代码全部消失--出现原因及解决方法
    Installation failed with message INSTALL_FAILED_UID_CHANGED.--APK安装失败解决方法
    Android项目实战(三十):Fresco加载gif图片并播放
    Android项目实战(十):自定义倒计时的TextView
    坚持一段时间了,达不到原有的目的,就应该放弃了——考研每日总结
    191006
  • 原文地址:https://www.cnblogs.com/xitang/p/2599479.html
Copyright © 2011-2022 走看看