Android 內存溢出解決方案(OOM) 整理總結


2012-03-13 01:24:07

標籤:Android加載大 Android 移動開發

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://mzh3344258.blog.51cto.com/1823534/804237

     在最近做的工程中發現加載的圖片太多或圖片過大時經常出現OOM問題,找網上資料也提供了很多方法,但自己感覺有點亂,特此,今天在不同型號的三款安卓手機上做了測試,因爲有效果也有結果,今天小馬就做個詳細的總結,以供朋友們共同交流學習,也供自己以後在解決OOM問題上有所提高,提前講下,片幅有點長,涉及的東西太多,大家耐心看,肯定有收穫的,裏面的很多東西小馬也是學習參考網絡資料使用的,先來簡單講下下:

   一般我們大家在遇到內存問題的時候常用的方式網上也有相關資料,大體如下幾種:

   一:在內存引用上做些處理,常用的有軟引用、強化引用、弱引用

   二:在內存中加載圖片時直接在內存中做處理,如:邊界壓縮

   三:動態回收內存

   四:優化Dalvik虛擬機的堆內存分配

   五:自定義堆內存大小

   可是真的有這麼簡單嗎,就用以上方式就能解決OOM了?不是的,繼續來看...

   下面小馬就照着上面的次序來整理下解決的幾種方式,數字序號與上面對應:

   1:軟引用(SoftReference)、虛引用(PhantomRefrence)、弱引用(WeakReference),這三個類是對heap中java對象的應用,通過這個三個類可以和gc做簡單的交互,除了這三個以外還有一個是最常用的強引用

    1.1:強引用,例如下面代碼:

Object o=new Object();       Object o1=o;

     上面代碼中第一句是在heap堆中創建新的Object對象通過o引用這個對象,第二句是通過o建立o1到new Object()這個heap堆中的對象的引用,這兩個引用都是強引用.只要存在對heap中對象的引用,gc就不會收集該對象.如果通過如下代碼:

o=null;       o1=null

      heap中對象有強可及對象、軟可及對象、弱可及對象、虛可及對象和不可到達對象。應用的強弱順序是強、軟、弱、和虛。對於對象是屬於哪種可及的對象,由他的最強的引用決定。如下:

String abc=new String("abc");  //1       SoftReference<String> abcSoftRef=new SoftReference<String>(abc);  //2       WeakReference<String> abcWeakRef = new WeakReference<String>(abc); //3       abc=null; //4       abcSoftRef.clear();//5

上面的代碼中:

    第一行在heap對中創建內容爲“abc”的對象,並建立abc到該對象的強引用,該對象是強可及的。第二行和第三行分別建立對heap中對象的軟引用和弱引用,此時heap中的對象仍是強可及的。第四行之後heap中對象不再是強可及的,變成軟可及的。同樣第五行執行之後變成弱可及的。

        1.2:軟引用

               軟引用是主要用於內存敏感的高速緩存。在jvm報告內存不足之前會清除所有的軟引用,這樣以來gc就有可能收集軟可及的對象,可能解決內存吃緊問題,避免內存溢出。什麼時候會被收集取決於gc的算法和gc運行時可用內存的大小。當gc決定要收集軟引用是執行以下過程,以上面的abcSoftRef爲例:

 

    1 首先將abcSoftRef的referent設置爲null,不再引用heap中的new String("abc")對象。

    2 將heap中的new String("abc")對象設置爲可結束的(finalizable)。

    3 當heap中的new String("abc")對象的finalize()方法被運行而且該對象佔用的內存被釋放, abcSoftRef被添加到它的ReferenceQueue中。

   注:對ReferenceQueue軟引用和弱引用可以有可無,但是虛引用必須有,參見:

Reference(T paramT, ReferenceQueue<? super T>paramReferenceQueue)

         被 Soft Reference 指到的對象,即使沒有任何 Direct Reference,也不會被清除。一直要到 JVM 內存不足且 沒有 Direct Reference 時纔會清除,SoftReference 是用來設計 object-cache 之用的。如此一來 SoftReference 不但可以把對象 cache 起來,也不會造成內存不足的錯誤 (OutOfMemoryError)。我覺得 Soft Reference 也適合拿來實作 pooling 的技巧。 

 A obj = new A();    Refenrence sr = new SoftReference(obj);       //引用時    if(sr!=null){        obj = sr.get();    }else{        obj = new A();        sr = new SoftReference(obj);    }

    1.3:弱引用

                當gc碰到弱可及對象,並釋放abcWeakRef的引用,收集該對象。但是gc可能需要對此運用才能找到該弱可及對象。通過如下代碼可以了明瞭的看出它的作用:

String abc=new String("abc");       WeakReference<String> abcWeakRef = new WeakReference<String>(abc);       abc=null;       System.out.println("before gc: "+abcWeakRef.get());       System.gc();       System.out.println("after gc: "+abcWeakRef.get());

運行結果:    

before gc: abc    

after gc: null   

     gc收集弱可及對象的執行過程和軟可及一樣,只是gc不會根據內存情況來決定是不是收集該對象。如果你希望能隨時取得某對象的信息,但又不想影響此對象的垃圾收集,那麼你應該用 Weak Reference 來記住此對象,而不是用一般的 reference。   

 

A obj = new A();           WeakReference wr = new WeakReference(obj);           obj = null;           //等待一段時間,obj對象就會被垃圾回收     ...         if (wr.get()==null) {      System.out.println("obj 已經被清除了 ");      } else {      System.out.println("obj 尚未被清除,其信息是 "+obj.toString());     }     ...   }

 

    在此例中,透過 get() 可以取得此 Reference 的所指到的對象,如果返回值爲 null 的話,代表此對象已經被清除。這類的技巧,在設計 Optimizer 或 Debugger 這類的程序時常會用到,因爲這類程序需要取得某對象的信息,但是不可以 影響此對象的垃圾收集。

 

     1.4:虛引用

 

     就是沒有的意思,建立虛引用之後通過get方法返回結果始終爲null,通過源代碼你會發現,虛引用通向會把引用的對象寫進referent,只是get方法返回結果爲null.先看一下和gc交互的過程在說一下他的作用. 

      1.4.1 不把referent設置爲null, 直接把heap中的new String("abc")對象設置爲可結束的(finalizable).

      1.4.2 與軟引用和弱引用不同, 先把PhantomRefrence對象添加到它的ReferenceQueue中.然後在釋放虛可及的對象. 

   你會發現在收集heap中的new String("abc")對象之前,你就可以做一些其他的事情.通過以下代碼可以瞭解他的作用.

 

import java.lang.ref.PhantomReference;       import java.lang.ref.Reference;       import java.lang.ref.ReferenceQueue;       import java.lang.reflect.Field;             public class Test {           public static boolean isRun = true;                 public static void main(String[] args) throws Exception {               String abc = new String("abc");               System.out.println(abc.getClass() + "@" + abc.hashCode());               final ReferenceQueue referenceQueue = new ReferenceQueue<String>();               new Thread() {                   public void run() {                       while (isRun) {                           Object o = referenceQueue.poll();                           if (o != null) {                               try {                                   Field rereferent = Reference.class                                          .getDeclaredField("referent");                                   rereferent.setAccessible(true);                                   Object result = rereferent.get(o);                                   System.out.println("gc will collect:"                                          + result.getClass() + "@"                                          + result.hashCode());                               } catch (Exception e) {                                         e.printStackTrace();                               }                           }                       }                   }               }.start();               PhantomReference<String> abcWeakRef = new PhantomReference<String>(abc,                       referenceQueue);               abc = null;               Thread.currentThread().sleep(3000);               System.gc();               Thread.currentThread().sleep(3000);               isRun = false;           }             }

 

 

結果爲

class java.lang.String@96354   

gc will collect:class java.lang.String@96354  好了,關於引用就講到這,下面看2

 

   2:在內存中壓縮小馬做了下測試,對於少量不太大的圖片這種方式可行,但太多而又大的圖片小馬用個笨的方式就是,先在內存中壓縮,再用軟引用避免OOM,兩種方式代碼如下,大家可參考下:

     方式一代碼如下:

@SuppressWarnings("unused") private Bitmap copressImage(String imgPath){     File picture = new File(imgPath);     Options bitmapFactoryOptions = new BitmapFactory.Options();     //下面這個設置是將圖片邊界不可調節變爲可調節     bitmapFactoryOptions.inJustDecodeBounds = true;     bitmapFactoryOptions.inSampleSize = 2;     int outWidth  = bitmapFactoryOptions.outWidth;     int outHeight = bitmapFactoryOptions.outHeight;     bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),          bitmapFactoryOptions);     float p_w_picpathw = 150;     float p_w_picpathh = 150;     int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight             / p_w_picpathh);     int xRatio = (int) Math             .ceil(bitmapFactoryOptions.outWidth / p_w_picpathw);     if (yRatio > 1 || xRatio > 1) {         if (yRatio > xRatio) {             bitmapFactoryOptions.inSampleSize = yRatio;         } else {             bitmapFactoryOptions.inSampleSize = xRatio;         }      }      bitmapFactoryOptions.inJustDecodeBounds = false;     bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),             bitmapFactoryOptions);     if(bmap != null){                        //ivwCouponImage.setImageBitmap(bmap);         return bmap;     }     return null; }

     方式二代碼如下:

package com.lvguo.scanstreet.activity;  import java.io.File; import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; import com.lvguo.scanstreet.R; import com.lvguo.scanstreet.data.ApplicationData; /**   * @Title: PhotoScanActivity.java * @Description: 照片預覽控制類 * @author XiaoMa   */ public class PhotoScanActivity extends Activity {     private Gallery gallery ;     private List<String> ImageList;     private List<String> it ;     private ImageAdapter adapter ;      private String path ;     private String shopType;     private HashMap<String, SoftReference<Bitmap>> p_w_picpathCache = null;     private Bitmap bitmap = null;     private SoftReference<Bitmap> srf = null;          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,          WindowManager.LayoutParams.FLAG_FULLSCREEN);          setContentView(R.layout.photoscan);         Intent intent = this.getIntent();         if(intent != null){             if(intent.getBundleExtra("bundle") != null){                 Bundle bundle = intent.getBundleExtra("bundle");                 path = bundle.getString("path");                 shopType = bundle.getString("shopType");             }         }         init();     }          private void init(){         p_w_picpathCache = new HashMap<String, SoftReference<Bitmap>>();          gallery = (Gallery)findViewById(R.id.gallery);          ImageList = getSD();          if(ImageList.size() == 0){             Toast.makeText(getApplicationContext(), "無照片,請返回拍照後再使用預覽", Toast.LENGTH_SHORT).show();             return ;          }          adapter = new ImageAdapter(this, ImageList);          gallery.setAdapter(adapter);          gallery.setOnItemLongClickListener(longlistener);     }           /**      * Gallery長按事件操作實現      */     private OnItemLongClickListener longlistener = new OnItemLongClickListener() {          @Override         public boolean onItemLongClick(AdapterView<?> parent, View view,                 final int position, long id) {             //此處添加長按事件刪除照片實現             AlertDialog.Builder dialog = new AlertDialog.Builder(PhotoScanActivity.this);             dialog.setIcon(R.drawable.warn);             dialog.setTitle("刪除提示");             dialog.setMessage("你確定要刪除這張照片嗎?");             dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int which) {                     File file = new File(it.get(position));                     boolean isSuccess;                     if(file.exists()){                         isSuccess = file.delete();                         if(isSuccess){                             ImageList.remove(position);                             adapter.notifyDataSetChanged();                             //gallery.setAdapter(adapter);                             if(ImageList.size() == 0){                                 Toast.makeText(getApplicationContext(), getResources().getString(R.string.phoSizeZero), Toast.LENGTH_SHORT).show();                             }                             Toast.makeText(getApplicationContext(), getResources().getString(R.string.phoDelSuccess), Toast.LENGTH_SHORT).show();                         }                     }                 }             });             dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int which) {                     dialog.dismiss();                 }             });             dialog.create().show();             return false;         }     };          /**      * 獲取SD卡上的所有圖片文件      * @return      */     private List<String> getSD() {         /* 設定目前所在路徑 */         File fileK ;         it = new ArrayList<String>();         if("newadd".equals(shopType)){               //如果是從查看本人新增列表項或商戶列表項進來時             fileK = new File(ApplicationData.TEMP);         }else{             //此時爲純粹新增             fileK = new File(path);         }         File[] files = fileK.listFiles();         if(files != null && files.length>0){             for(File f : files ){                 if(getImageFile(f.getName())){                     it.add(f.getPath());                                                               Options bitmapFactoryOptions = new BitmapFactory.Options();                                         //下面這個設置是將圖片邊界不可調節變爲可調節                     bitmapFactoryOptions.inJustDecodeBounds = true;                     bitmapFactoryOptions.inSampleSize = 5;                     int outWidth  = bitmapFactoryOptions.outWidth;                     int outHeight = bitmapFactoryOptions.outHeight;                     float p_w_picpathw = 150;                     float p_w_picpathh = 150;                     int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight                             / p_w_picpathh);                     int xRatio = (int) Math                             .ceil(bitmapFactoryOptions.outWidth / p_w_picpathw);                     if (yRatio > 1 || xRatio > 1) {                         if (yRatio > xRatio) {                             bitmapFactoryOptions.inSampleSize = yRatio;                         } else {                             bitmapFactoryOptions.inSampleSize = xRatio;                         }                      }                      bitmapFactoryOptions.inJustDecodeBounds = false;                                          bitmap = BitmapFactory.decodeFile(f.getPath(),                             bitmapFactoryOptions);                                          //bitmap = BitmapFactory.decodeFile(f.getPath());                      srf = new SoftReference<Bitmap>(bitmap);                     p_w_picpathCache.put(f.getName(), srf);                 }             }         }         return it;     }          /**      * 獲取圖片文件方法的具體實現       * @param fName      * @return      */     private boolean getImageFile(String fName) {         boolean re;          /* 取得擴展名 */         String end = fName                 .substring(fName.lastIndexOf(".") + 1, fName.length())                 .toLowerCase();          /* 按擴展名的類型決定MimeType */         if (end.equals("jpg") || end.equals("gif") || end.equals("png")                 || end.equals("jpeg") || end.equals("bmp")) {             re = true;         } else {             re = false;         }         return re;     }          public class ImageAdapter extends BaseAdapter{         /* 聲明變量 */         int mGalleryItemBackground;         private Context mContext;         private List<String> lis;                  /* ImageAdapter的構造符 */         public ImageAdapter(Context c, List<String> li) {             mContext = c;             lis = li;             TypedArray a = obtainStyledAttributes(R.styleable.Gallery);             mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);             a.recycle();         }          /* 幾定要重寫的方法getCount,傳回圖片數目 */         public int getCount() {             return lis.size();         }          /* 一定要重寫的方法getItem,傳回position */         public Object getItem(int position) {             return lis.get(position);         }          /* 一定要重寫的方法getItemId,傳並position */         public long getItemId(int position) {             return position;         }                  /* 幾定要重寫的方法getView,傳並幾View對象 */         public View getView(int position, View convertView, ViewGroup parent) {             System.out.println("lis:"+lis);             File file = new File(it.get(position));             SoftReference<Bitmap> srf = p_w_picpathCache.get(file.getName());             Bitmap bit = srf.get();             ImageView i = new ImageView(mContext);             i.setImageBitmap(bit);             i.setScaleType(ImageView.ScaleType.FIT_XY);             i.setLayoutParams( new Gallery.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,                     WindowManager.LayoutParams.WRAP_CONTENT));             return i;         }     } }

    上面兩種方式第一種直接使用邊界壓縮,第二種在使用邊界壓縮的情況下間接的使用了軟引用來避免OOM,但大家都知道,這些函數在完成decode後,最終都是通過java層的createBitmap來完成的,需要消耗更多內存,如果圖片多且大,這種方式還是會引用OOM異常的,不着急,有的是辦法解決,繼續看,以下方式也大有妙用的:

1. InputStream is = this.getResources().openRawResource(R.drawable.pic1);      BitmapFactory.Options options=new BitmapFactory.Options();      options.inJustDecodeBounds = false;      options.inSampleSize = 10;   //width,hight設爲原來的十分一      Bitmap btp =BitmapFactory.decodeStream(is,null,options); 2. if(!bmp.isRecycle() ){          bmp.recycle()   //回收圖片所佔的內存          system.gc()  //提醒系統及時回收 } 上面代碼與下面代碼大家可分開使用,也可有效緩解內存問題哦...吼吼...     /** 這個地方大家別搞混了,爲了方便小馬把兩個貼一起了,使用的時候記得分開使用     * 以最省內存的方式讀取本地資源的圖片      */       public static Bitmap readBitMap(Context context, int resId){           BitmapFactory.Options opt = new BitmapFactory.Options();           opt.inPreferredConfig = Bitmap.Config.RGB_565;           opt.inPurgeable = true;          opt.inInputShareable = true;             //獲取資源圖片          InputStream is = context.getResources().openRawResource(resId);              return BitmapFactory.decodeStream(is,null,opt);      }

   3:大家可以選擇在合適的地方使用以下代碼動態並自行顯式調用GC來回收內存:

if(bitmapObject.isRecycled()==false) //如果沒有回收            bitmapObject.recycle();

   4:這個就好玩了,優化Dalvik虛擬機的堆內存分配,聽着很強大,來看下具體是怎麼一回事

     對於Android平臺來說,其託管層使用的Dalvik JavaVM從目前的表現來看還有很多地方可以優化處理,比如我們在開發一些大型遊戲或耗資源的應用中可能考慮手動干涉GC處理,使用 dalvik.system.VMRuntime類提供的setTargetHeapUtilization方法可以增強程序堆內存的處理效率。當然具體原理我們可以參考開源工程,這裏我們僅說下使用方法: 代碼如下:

private final static floatTARGET_HEAP_UTILIZATION = 0.75f;  在程序onCreate時就可以調用 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 即可

   5:自定義我們的應用需要多大的內存,這個好暴力哇,強行設置最小內存大小,代碼如下:

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;  //設置最小heap內存爲6MB大小 VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);

    好了,文章寫完了,片幅有點長,因爲涉及到的東西太多了,其它文章小馬都會貼源碼,這篇文章小馬是直接在項目中用三款安卓真機測試的,有效果,項目原碼就不在這貼了,不然泄密了都,吼吼,但這裏講下還是會因爲手機的不同而不同,大家得根據自己需求選擇合適的方式來避免OOM,大家加油呀,每天都有或多或少的收穫,這也算是進步,加油加油!

本文出自 “酷_莫名簡單、KNothing” 博客,請務必保留此出處http://mzh3344258.blog.51cto.com/1823534/804237


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章