Java的四種對(duì)象引用類(lèi)型:強(qiáng)引用、弱引用账嚎、軟引用、虛引用儡蔓。
強(qiáng)引用(StrongReference):
強(qiáng)引用是使用最普遍的引用郭蕉。如果一個(gè)對(duì)象具有強(qiáng)引用,那垃圾回收器絕不會(huì)回收它喂江。當(dāng)內(nèi)存空間不足召锈,Java虛擬機(jī)寧愿拋出OutOfMemoryError錯(cuò)誤,使程序異常終止获询,也不會(huì)靠隨意回收具有強(qiáng)引用的對(duì)象來(lái)解決內(nèi)存不足的問(wèn)題涨岁。
軟引用(SoftReference):
如果一個(gè)對(duì)象只具有軟引用,則內(nèi)存空間足夠吉嚣,垃圾回收器就不會(huì)回收它梢薪;如果內(nèi)存空間不足了,就會(huì)回收這些對(duì)象的內(nèi)存尝哆。只要垃圾回收器沒(méi)有回收它秉撇,該對(duì)象就可以被程序使用。軟引用可用來(lái)實(shí)現(xiàn)內(nèi)存敏感的高速緩存(下文給出示例)秋泄。
軟引用可以和一個(gè)引用隊(duì)列(ReferenceQueue)聯(lián)合使用琐馆,如果軟引用所引用的對(duì)象被垃圾回收器回收,Java虛擬機(jī)就會(huì)把這個(gè)軟引用加入到與之關(guān)聯(lián)的引用隊(duì)列中恒序。
弱引用(WeakReference):
弱引用與軟引用的區(qū)別在于:只具有弱引用的對(duì)象擁有更短暫的生命周期瘦麸。在垃圾回收器線程掃描它所管轄的內(nèi)存區(qū)域的過(guò)程中,一旦發(fā)現(xiàn)了只具有弱引用的對(duì)象歧胁,不管當(dāng)前內(nèi)存空間足夠與否滋饲,都會(huì)回收它的內(nèi)存。不過(guò)喊巍,由于垃圾回收器是一個(gè)優(yōu)先級(jí)很低的線程屠缭,因此不一定會(huì)很快發(fā)現(xiàn)那些只具有弱引用的對(duì)象。
弱引用可以和一個(gè)引用隊(duì)列(ReferenceQueue)聯(lián)合使用玄糟,如果弱引用所引用的對(duì)象被垃圾回收勿她,Java虛擬機(jī)就會(huì)把這個(gè)弱引用加入到與之關(guān)聯(lián)的引用隊(duì)列中袄秩。
虛引用(PhantomReference):
“虛引用”顧名思義阵翎,就是形同虛設(shè)逢并,與其他幾種引用都不同,虛引用并不會(huì)決定對(duì)象的生命周期郭卫。如果一個(gè)對(duì)象僅持有虛引用砍聊,那么它就和沒(méi)有任何引用一樣,在任何時(shí)候都可能被垃圾回收器回收贰军。
虛引用主要用來(lái)跟蹤對(duì)象被垃圾回收器回收的活動(dòng)玻蝌。虛引用與軟引用和弱引用的一個(gè)區(qū)別在于:虛引用必須和引用隊(duì)列 (ReferenceQueue)聯(lián)合使用。當(dāng)垃圾回收器準(zhǔn)備回收一個(gè)對(duì)象時(shí)词疼,如果發(fā)現(xiàn)它還有虛引用俯树,就會(huì)在回收對(duì)象的內(nèi)存之前,把這個(gè)虛引用加入到與之 關(guān)聯(lián)的引用隊(duì)列中贰盗。
案例實(shí)踐:
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();
}
}
運(yùn)行結(jié)果
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
從程序運(yùn)行結(jié)果可以看出许饿,虛引用形同虛設(shè),它所引用的對(duì)象隨時(shí)可能被垃圾回收器回收舵盈,具有弱引用的對(duì)象擁有稍微長(zhǎng)一點(diǎn)的生命周期陋率,當(dāng)垃圾回收器執(zhí)行回收操作時(shí),有可能被垃圾回收器回收秽晚,具有軟引用的對(duì)象擁有更長(zhǎng)的生命周期瓦糟,但在Java虛擬機(jī)認(rèn)為內(nèi)存不足的情況下,也是會(huì)被垃圾回收器回收的赴蝇。