欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程之內(nèi)存溢出解決方案(OOM)實例總結(jié)

 更新時間:2015年11月23日 11:22:22   作者:sengeiou  
這篇文章主要介紹了Android編程之內(nèi)存溢出解決方案(OOM),結(jié)合實例實例總結(jié)分析了Android編程過程中常見的內(nèi)存溢出情況與對應(yīng)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例總結(jié)了Android編程之內(nèi)存溢出解決方案(OOM)。分享給大家供大家參考,具體如下:

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

一般我們大家在遇到內(nèi)存問題的時候常用的方式網(wǎng)上也有相關(guān)資料,大體如下幾種:

一:在內(nèi)存引用上做些處理,常用的有軟引用、強化引用、弱引用
二:在內(nèi)存中加載圖片時直接在內(nèi)存中做處理,如:邊界壓縮
三:動態(tài)回收內(nèi)存
四:優(yōu)化Dalvik虛擬機的堆內(nèi)存分配
五:自定義堆內(nèi)存大小

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

下面小馬就照著上面的次序來整理下解決的幾種方式,數(shù)字序號與上面對應(yīng):

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

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

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

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

o=null;   
o1=null

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

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對中創(chuàng)建內(nèi)容為“abc”的對象,并建立abc到該對象的強引用,該對象是強可及的。第二行和第三行分別建立對heap中對象的軟引用和弱引用,此時heap中的對象仍是強可及的。第四行之后heap中對象不再是強可及的,變成軟可及的。同樣第五行執(zhí)行之后變成弱可及的。

1.2:軟引用

軟引用是主要用于內(nèi)存敏感的高速緩存。在jvm報告內(nèi)存不足之前會清除所有的軟引用,這樣以來gc就有可能收集軟可及的對象,可能解決內(nèi)存吃緊問題,避免內(nèi)存溢出。什么時候會被收集取決于gc的算法和gc運行時可用內(nèi)存的大小。當(dāng)gc決定要收集軟引用是執(zhí)行以下過程,以上面的abcSoftRef為例:
 
1 首先將abcSoftRef的referent設(shè)置為null,不再引用heap中的new String("abc")對象。

2 將heap中的new String("abc")對象設(shè)置為可結(jié)束的(finalizable)。

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

注:對ReferenceQueue軟引用和弱引用可以有可無,但是虛引用必須有,參見:
Reference(T paramT, ReferenceQueue<? super T>paramReferenceQueue)

被 Soft Reference 指到的對象,即使沒有任何 Direct Reference,也不會被清除。一直要到 JVM 內(nèi)存不足且 沒有 Direct Reference 時才會清除,SoftReference 是用來設(shè)計 object-cache 之用的。如此一來 SoftReference 不但可以把對象 cache 起來,也不會造成內(nèi)存不足的錯誤 (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:弱引用

當(dāng)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());  

運行結(jié)果:

before gc: abc  
after gc: null 

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

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

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

1.4:虛引用

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

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

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

你會發(fā)現(xiàn)在收集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;   
  }   
}

結(jié)果為

class java.lang.String@96354 
gc will collect:class java.lang.String@96354

 好了,關(guān)于引用就講到這,下面看2
 

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

方式一代碼如下:

@SuppressWarnings("unused")
private Bitmap copressImage(String imgPath){
  File picture = new File(imgPath);
  Options bitmapFactoryOptions = new BitmapFactory.Options();
  //下面這個設(shè)置是將圖片邊界不可調(diào)節(jié)變?yōu)榭烧{(diào)節(jié)
  bitmapFactoryOptions.inJustDecodeBounds = true;
  bitmapFactoryOptions.inSampleSize = 2;
  int outWidth = bitmapFactoryOptions.outWidth;
  int outHeight = bitmapFactoryOptions.outHeight;
  bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
     bitmapFactoryOptions);
  float imagew = 150;
  float imageh = 150;
  int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight
      / imageh);
  int xRatio = (int) Math
      .ceil(bitmapFactoryOptions.outWidth / imagew);
  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: 照片預(yù)覽控制類
* @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>> imageCache = 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(){
    imageCache = new HashMap<String, SoftReference<Bitmap>>();
     gallery = (Gallery)findViewById(R.id.gallery);
     ImageList = getSD();
     if(ImageList.size() == 0){
      Toast.makeText(getApplicationContext(), "無照片,請返回拍照后再使用預(yù)覽", Toast.LENGTH_SHORT).show();
      return ;
     }
     adapter = new ImageAdapter(this, ImageList);
     gallery.setAdapter(adapter);
     gallery.setOnItemLongClickListener(longlistener);
  }
  /**
   * Gallery長按事件操作實現(xiàn)
   */
  private OnItemLongClickListener longlistener = new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
        final int position, long id) {
      //此處添加長按事件刪除照片實現(xiàn)
      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() {
    /* 設(shè)定目前所在路徑 */
    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();
          //下面這個設(shè)置是將圖片邊界不可調(diào)節(jié)變?yōu)榭烧{(diào)節(jié)
          bitmapFactoryOptions.inJustDecodeBounds = true;
          bitmapFactoryOptions.inSampleSize = 5;
          int outWidth = bitmapFactoryOptions.outWidth;
          int outHeight = bitmapFactoryOptions.outHeight;
          float imagew = 150;
          float imageh = 150;
          int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight
              / imageh);
          int xRatio = (int) Math
              .ceil(bitmapFactoryOptions.outWidth / imagew);
          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);
          imageCache.put(f.getName(), srf);
        }
      }
    }
    return it;
  }
  /**
   * 獲取圖片文件方法的具體實現(xiàn) 
   * @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的構(gòu)造符 */
    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,傳回圖片數(shù)目 */
    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 = imageCache.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,但大家都知道,這些函數(shù)在完成decode后,最終都是通過java層的createBitmap來完成的,需要消耗更多內(nèi)存,如果圖片多且大,這種方式還是會引用OOM異常的,不著急,有的是辦法解決,繼續(xù)看,以下方式也大有妙用的:

1.

InputStream is = this.getResources().openRawResource(R.drawable.pic1);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10;  //width,hight設(shè)為原來的十分一
Bitmap btp =BitmapFactory.decodeStream(is,null,options);

2.

if(!bmp.isRecycle() ){
     bmp.recycle()  //回收圖片所占的內(nèi)存
     system.gc() //提醒系統(tǒng)及時回收
}

上面代碼與下面代碼大家可分開使用,也可有效緩解內(nèi)存問題哦...吼吼...

/** 這個地方大家別搞混了,為了方便小馬把兩個貼一起了,使用的時候記得分開使用
* 以最省內(nèi)存的方式讀取本地資源的圖片
*/ 
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:大家可以選擇在合適的地方使用以下代碼動態(tài)并自行顯式調(diào)用GC來回收內(nèi)存:

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

4:這個就好玩了,優(yōu)化Dalvik虛擬機的堆內(nèi)存分配,聽著很強大,來看下具體是怎么一回事

對于Android平臺來說,其托管層使用的Dalvik JavaVM從目前的表現(xiàn)來看還有很多地方可以優(yōu)化處理,比如我們在開發(fā)一些大型游戲或耗資源的應(yīng)用中可能考慮手動干涉GC處理,使用 dalvik.system.VMRuntime類提供的setTargetHeapUtilization方法可以增強程序堆內(nèi)存的處理效率。當(dāng)然具體原理我們可以參考開源工程,這里我們僅說下使用方法: 代碼如下:

復(fù)制代碼 代碼如下:
private final static floatTARGET_HEAP_UTILIZATION = 0.75f;

在程序onCreate時就可以調(diào)用
復(fù)制代碼 代碼如下:
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

即可

5:自定義我們的應(yīng)用需要多大的內(nèi)存,這個好暴力哇,強行設(shè)置最小內(nèi)存大小,代碼如下:

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

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

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論