更新時(shí)間:2022-05-11 10:48:44 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2149次
在 Java 中,有四種類型的引用在它們被垃圾收集的方式上有所不同。
1.強(qiáng)引用
2.弱引用
3.軟引用
4.幻影參考
這是引用對(duì)象的默認(rèn)類型/類。任何具有活動(dòng)強(qiáng)引用的對(duì)象都沒(méi)有資格進(jìn)行垃圾回收。只有當(dāng)被強(qiáng)引用的變量指向 null 時(shí),對(duì)象才會(huì)被垃圾回收。
MyClass obj = new MyClass();
這里的 'obj' 對(duì)象是對(duì)新創(chuàng)建的 MyClass 實(shí)例的強(qiáng)引用,當(dāng)前 obj 是活動(dòng)對(duì)象,因此不能被垃圾收集。
對(duì)象=空;
//'obj' 對(duì)象不再引用實(shí)例。
所以'MyClass 類型對(duì)象現(xiàn)在可用于垃圾收集。

// Java program to illustrate Strong reference
class Gfg
{
//Code..
}
public class Example
{
public static void main(String[] args)
{
//Strong Reference - by default
Gfg g = new Gfg();
//Now, object to which 'g' was pointing earlier is
//eligible for garbage collection.
g = null;
}
}
弱引用對(duì)象不是引用對(duì)象的默認(rèn)類型/類,在使用它們時(shí)應(yīng)明確指定。
這種類型的引用在 WeakHashMap 中用于引用條目對(duì)象。
如果 JVM 檢測(cè)到一個(gè)只有弱引用的對(duì)象(即沒(méi)有強(qiáng)或軟引用鏈接到任何對(duì)象對(duì)象),該對(duì)象將被標(biāo)記為垃圾回收。
要?jiǎng)?chuàng)建此類引用,使用java.lang.ref.WeakReference類。
這些引用在實(shí)時(shí)應(yīng)用程序中使用,同時(shí)建立一個(gè) DBConnection,當(dāng)使用數(shù)據(jù)庫(kù)的應(yīng)用程序關(guān)閉時(shí),垃圾收集器可能會(huì)清理該 DBConnection。

//Java Code to illustrate Weak reference
import java.lang.ref.WeakReference;
class Gfg
{
//code
public void x()
{
System.out.println("GeeksforGeeks");
}
}
public class Example
{
public static void main(String[] args)
{
// Strong Reference
Gfg g = new Gfg();
g.x();
// Creating Weak Reference to Gfg-type object to which 'g'
// is also pointing.
WeakReference<Gfg> weakref = new WeakReference<Gfg>(g);
//Now, Gfg-type object to which 'g' was pointing earlier
//is available for garbage collection.
//But, it will be garbage collected only when JVM needs memory.
g = null;
// You can retrieve back the object which
// has been weakly referenced.
// It successfully calls the method.
g = weakref.get();
g.x();
}
}
輸出:
GeeksforGeeks
GeeksforGeeks
可以招募兩種不同級(jí)別的弱點(diǎn):軟弱點(diǎn)和幻影弱點(diǎn)
在軟引用中,即使對(duì)象可以進(jìn)行垃圾回收,也不會(huì)被垃圾回收,直到 JVM 嚴(yán)重需要內(nèi)存。當(dāng) JVM 內(nèi)存不足時(shí),對(duì)象會(huì)從內(nèi)存中清除。創(chuàng)建這樣的引用使用java.lang.ref.SoftReference類。

//Code to illustrate Soft reference
import java.lang.ref.SoftReference;
class Gfg
{
//code..
public void x()
{
System.out.println("GeeksforGeeks");
}
}
public class Example
{
public static void main(String[] args)
{
// Strong Reference
Gfg g = new Gfg();
g.x();
// Creating Soft Reference to Gfg-type object to which 'g'
// is also pointing.
SoftReference<Gfg> softref = new SoftReference<Gfg>(g);
// Now, Gfg-type object to which 'g' was pointing
// earlier is available for garbage collection.
g = null;
// You can retrieve back the object which
// has been weakly referenced.
// It successfully calls the method.
g = softref.get();
g.x();
}
}
輸出:
GeeksforGeeks
GeeksforGeeks
幻影引用所引用的對(duì)象符合垃圾回收條件。但是,在將它們從內(nèi)存中刪除之前,JVM 會(huì)將它們放入一個(gè)名為 'reference queue' 的隊(duì)列中。在對(duì)它們調(diào)用 finalize() 方法后將它們放入引用隊(duì)列中。使用java.lang.ref.PhantomReference類創(chuàng)建此類引用。
//Code to illustrate Phantom reference
import java.lang.ref.*;
class Gfg
{
//code
public void x()
{
System.out.println("GeeksforGeeks");
}
}
public class Example
{
public static void main(String[] args)
{
//Strong Reference
Gfg g = new Gfg();
g.x();
//Creating reference queue
ReferenceQueue<Gfg> refQueue = new ReferenceQueue<Gfg>();
//Creating Phantom Reference to Gfg-type object to which 'g'
//is also pointing.
PhantomReference<Gfg> phantomRef = null;
phantomRef = new PhantomReference<Gfg>(g,refQueue);
//Now, Gfg-type object to which 'g' was pointing
//earlier is available for garbage collection.
//But, this object is kept in 'refQueue' before
//removing it from the memory.
g = null;
//It always returns null.
g = phantomRef.get();
//It shows NullPointerException.
g.x();
}
}
運(yùn)行時(shí)錯(cuò)誤:
線程“主”java.lang.NullPointerException 中的異常
在 Example.main(Example.java:31)
輸出:
GeeksforGeeks
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)