http://mingnjintian-163-com.iteye.com/blog/1120472
weakReference一般用來防止內存泄漏,要保證內存被VM回收?
softReference的話,好像多用作來實現cache機制.
?
WeakReference:?
弱引用對象,它們并不禁止其指示對象變得可終結,并被終結,然后被回收。弱引用最常用于實現規范化的映射。 ?
假定垃圾回收器確定在某一時間點上某個對象是弱可到達對象。這時,它將自動清除針對此對象的所有弱引用,以及通過強引用鏈和軟引用,可以從其到達該對象的針對任何其他弱可到達對象的所有弱引用。同時它將聲明所有以前的弱可到達對象為可終結的。在同一時間或晚些時候,它將那些已經向引用隊列注冊的新清除的弱引用加入隊列。 ??
///?
SoftReference:?
軟引用對象,在響應內存需要時,由垃圾回收器決定是否清除此對象。軟引用對象最常用于實現內存敏感的緩存。 ?
假定垃圾回收器確定在某一時間點某個對象是軟可到達對象。這時,它可以選擇自動清除針對該對象的所有軟引用,以及通過強引用鏈,從其可以到達該對象的針對任何其他軟可到達對象的所有軟引用。在同一時間或晚些時候,它會將那些已經向引用隊列注冊的新清除的軟引用加入隊列。 ??
軟可到達對象的所有軟引用都要保證在虛擬機拋出 ? OutOfMemoryError ? 之前已經被清除。否則,清除軟引用的時間或者清除不同對象的一組此類引用的順序將不受任何約束。然而,虛擬機實現不鼓勵清除最近訪問或使用過的軟引用。 ??
此類的直接實例可用于實現簡單緩存;該類或其派生的子類還可用于更大型的數據結構,以實現更復雜的緩存。只要軟引用的指示對象是強可到達對象,即正在實際使用的對象,就不會清除軟引用。例如,通過保持最近使用的項的強指示對象,并由垃圾回收器決定是否放棄剩余的項,復雜的緩存可以防止放棄最近使用的項?
?
?
?
Java內存管理之軟引用(Soft Reference)
?
軟引用(Soft ?Reference)的主要特點是具有較強的引用功能。只有當內存不夠的時候才回收這類內存,因此在內存足夠的時候,他們通常不被回收。另外,這些引用對象還能保證在Java ?拋出OutOfMemory異常之前,被設置為null。他可以用于實現一些常用資源的緩存,實現Cache的功能,保證最大限度的使用內存而不引起OutOfMemory異常。
? ? ?下面是軟引用的實現代碼:
?
?
Java代碼 ?
import?java.lang.ref.SoftReference; ??public?class?softReference?{ ??????public?static?void?main(String[]?args)?{ ??????????A?a?=?new?A(); ????????????????????a.test(); ?????????? ??????????SoftReference?sr?=?new?SoftReference(a); ??????????a?=?null; ????????????????????if?(sr?!=?null)?{ ??????????????a?=?(A)?sr.get(); ??????????????a.test(); ??????????}?else?{ ?????????????? ??????????????a?=?new?A(); ??????????????a.test(); ??????????????a?=?null; ??????????????sr?=?new?SoftReference(a); ??????????} ??????} ??} ??class?A?{ ??????public?void?test()?{ ??????????System.out.println("Soft?Reference?test"); ??????} ??}??
import java.lang.ref.SoftReference;
public class softReference {public static void main(String[] args) {A a = new A();// 使用aa.test();// 使用完了a,將它設置為soft引用類型,并且釋放強引用SoftReference sr = new SoftReference(a);a = null;// 下次使用if (sr != null) {a = (A) sr.get();a.test();} else {// GC由于低內存,已釋放a,因此需要重新裝載a = new A();a.test();a = null;sr = new SoftReference(a);}}
}
class A {public void test() {System.out.println("Soft Reference test");}
}
?
?
? 軟引用技術的引進使Java應用可以更好的管理內存,穩定系統,防止系統內存溢出,避免系統崩潰。因此在處理一些占用內存大而且聲明周期較長,但使用并不頻繁的對象時應盡量應用該技術。但事物總帶有兩面性的,有利也有弊,在某些時候對軟引用的使用會降低應用的運行效率與性能,例如:應用軟引用的對象的初始化過程較為耗時,或者對象的狀態在程序的運行過程中發生了變化,都會給重新創建對象與初始化對象帶來不同程度的麻煩,有些時候我們要權衡利弊擇時應用。
?
?
?
在android中可以巧妙的運用軟引用(SoftRefrence)(來源段落:http://winuxxan.blog.51cto.com/2779763/512180)
有些時候,我們使用Bitmap后沒有保留對它的引用,因此就無法調用Recycle函數。這時候巧妙的運用軟引用,可以使Bitmap在內存快不足時得到有效的釋放。如下例:
?
?
Java代碼 ?
private?class?MyAdapter?extends?BaseAdapter?{?? ????private?ArrayList<SoftReference<Bitmap>>?mBitmapRefs?=?new?ArrayList<SoftReference<Bitmap>>();?? ??private?ArrayList<Value>?mValues;?? ??private?Context?mContext;?? ??private?LayoutInflater?mInflater;?? ????MyAdapter(Context?context,?ArrayList<Value>?values)?{?? ??????mContext?=?context;?? ??????mValues?=?values;?? ??????mInflater?=?(LayoutInflater)?context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);?? ??}?? ??public?int?getCount()?{?? ??????return?mValues.size();?? ??}?? ????public?Object?getItem(int?i)?{?? ??????return?mValues.get(i);?? ??}?? ????public?long?getItemId(int?i)?{?? ??????return?i;?? ??}?? ????public?View?getView(int?i,?View?view,?ViewGroup?viewGroup)?{?? ??????View?newView?=?null;?? ??????if(view?!=?null)?{?? ??????????newView?=?view;?? ??????}?else?{?? ??????????newView?=(View)mInflater.inflate(R.layout.image_view,?false);?? ??????}?? ????????Bitmap?bitmap?=?BitmapFactory.decodeFile(mValues.get(i).fileName);?? ??????mBitmapRefs.add(new?SoftReference<Bitmap>(bitmap));???????????((ImageView)newView).setImageBitmap(bitmap);?? ????????return?newView;?? ??}?? ??}????
private class MyAdapter extends BaseAdapter { private ArrayList<SoftReference<Bitmap>> mBitmapRefs = new ArrayList<SoftReference<Bitmap>>();
private ArrayList<Value> mValues;
private Context mContext;
private LayoutInflater mInflater; MyAdapter(Context context, ArrayList<Value> values) { mContext = context; mValues = values; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() { return mValues.size();
} public Object getItem(int i) { return mValues.get(i);
} public long getItemId(int i) { return i;
} public View getView(int i, View view, ViewGroup viewGroup) { View newView = null; if(view != null) { newView = view; } else { newView =(View)mInflater.inflate(R.layout.image_view, false); } Bitmap bitmap = BitmapFactory.decodeFile(mValues.get(i).fileName); mBitmapRefs.add(new SoftReference<Bitmap>(bitmap)); //此處加入ArrayList ((ImageView)newView).setImageBitmap(bitmap); return newView;
}
}
?
?
?
?
?
?
?
?
綜合帖子1、?http://topic.csdn.net/t/20060327/23/4644203.html
帖子2?http://www.blogjava.net/ajie/archive/2005/12/18/24435.html
?
總結
以上是生活随笔為你收集整理的java弱引用(WeakReference)和SoftReference的区别以及在android内存处理的作用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。