Java的四種對象引用類型:強引用、弱引用、軟引用、虛引用。
強引用(StrongReference):
強引用是使用最普遍的引用。如果一個對象具有強引用,那垃圾回收器絕不會回收它。當內存空間不足,Java虛擬機寧愿拋出OutOfMemoryError錯誤,使程序異常終止,也不會靠隨意回收具有強引用的對象來解決內存不足的問題。
軟引用(SoftReference):
如果一個對象只具有軟引用,則內存空間足夠,垃圾回收器就不會回收它;如果內存空間不足了,就會回收這些對象的內存。只要垃圾回收器沒有回收它,該對象就可以被程序使用。軟引用可用來實現內存敏感的高速緩存(下文給出示例)。
軟引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果軟引用所引用的對象被垃圾回收器回收,Java虛擬機就會把這個軟引用加入到與之關聯的引用隊列中。
弱引用(WeakReference):
弱引用與軟引用的區別在于:只具有弱引用的對象擁有更短暫的生命周期。在垃圾回收器線程掃描它所管轄的內存區域的過程中,一旦發現了只具有弱引用的對象,不管當前內存空間足夠與否,都會回收它的內存。不過,由于垃圾回收器是一個優先級很低的線程,因此不一定會很快發現那些只具有弱引用的對象。
弱引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果弱引用所引用的對象被垃圾回收,Java虛擬機就會把這個弱引用加入到與之關聯的引用隊列中。
虛引用(PhantomReference):
“虛引用”顧名思義,就是形同虛設,與其他幾種引用都不同,虛引用并不會決定對象的生命周期。如果一個對象僅持有虛引用,那么它就和沒有任何引用一樣,在任何時候都可能被垃圾回收器回收。
虛引用主要用來跟蹤對象被垃圾回收器回收的活動。虛引用與軟引用和弱引用的一個區別在于:虛引用必須和引用隊列 (ReferenceQueue)聯合使用。當垃圾回收器準備回收一個對象時,如果發現它還有虛引用,就會在回收對象的內存之前,把這個虛引用加入到與之 關聯的引用隊列中。
案例實踐:
package com.example.learn;
import java.lang.ref.*;
import java.util.*;
class Grocery {
private static final int SIZE=10000;
private double[] d = new double[SIZE];
private String id;
public Grocery(String id) {
this.id = id;
}
public String toString(){
return id;
}
public void finalize(){
System.out.println("Finalizing ... "+id);
}
}
public class References {
private static ReferenceQueue<Grocery> rq = new ReferenceQueue<Grocery>();
public static void checkQueue(){
Reference<? extends Grocery> inq = rq.poll();
if(inq!=null){
System.out.println("In queue : "+inq.get());
}
}
public static void main(String[] args){
final int size = 10;
Set<SoftReference<Grocery>> sa = new HashSet<SoftReference<Grocery>>();
for(int i=0;i<size;i++){
SoftReference<Grocery> ref =
new SoftReference<Grocery>(new Grocery("Soft "+i),rq);
System.out.println("Just created: "+ref.get());
sa.add(ref);
}
System.gc();
checkQueue();
Set<WeakReference<Grocery>> wa = new HashSet<WeakReference<Grocery>>();
for(int i=0;i<size;i++){
WeakReference<Grocery> ref =
new WeakReference<Grocery>(new Grocery("Weak "+i),rq);
System.out.println("Just created: "+ref.get());
wa.add(ref);
}
System.gc();
checkQueue();
Set<PhantomReference<Grocery>> pa = new HashSet<PhantomReference<Grocery>>();
for(int i=0;i<size;i++) {
PhantomReference<Grocery> ref =
new PhantomReference<Grocery>(new Grocery("Phantom "+i),rq);
System.out.println("Just created: "+ref.get());
pa.add(ref);
}
System.gc();
checkQueue();
}
}
運行結果
Just created: Soft 0
Just created: Soft 1
Just created: Soft 2
Just created: Soft 3
Just created: Soft 4
Just created: Soft 5
Just created: Soft 6
Just created: Soft 7
Just created: Soft 8
Just created: Soft 9
Just created: Weak 0
Just created: Weak 1
Just created: Weak 2
Just created: Weak 3
Just created: Weak 4
Just created: Weak 5
Just created: Weak 6
Just created: Weak 7
Just created: Weak 8
Just created: Weak 9
Finalizing ... Weak 7
Just created: null
Finalizing ... Weak 2
Just created: null
Finalizing ... Weak 1
Just created: null
Finalizing ... Weak 0
Just created: null
Finalizing ... Weak 5
Just created: null
Finalizing ... Weak 4
Just created: null
Finalizing ... Weak 3
Finalizing ... Weak 6
Finalizing ... Weak 9
Finalizing ... Weak 8
Just created: null
Just created: null
Just created: null
Just created: null
Finalizing ... Phantom 2
In queue : null
Finalizing ... Phantom 1
Finalizing ... Phantom 0
Finalizing ... Phantom 4
Finalizing ... Phantom 3
Finalizing ... Phantom 6
Finalizing ... Phantom 5
Finalizing ... Phantom 9
Finalizing ... Phantom 8
Finalizing ... Phantom 7
從程序運行結果可以看出,虛引用形同虛設,它所引用的對象隨時可能被垃圾回收器回收,具有弱引用的對象擁有稍微長一點的生命周期,當垃圾回收器執行回收操作時,有可能被垃圾回收器回收,具有軟引用的對象擁有更長的生命周期,但在Java虛擬機認為內存不足的情況下,也是會被垃圾回收器回收的。