更新時(shí)間:2022-05-11 10:48:44 來源:動力節(jié)點(diǎn) 瀏覽1635次
在 Java 中,有四種類型的引用在它們被垃圾收集的方式上有所不同。
1.強(qiáng)引用
2.弱引用
3.軟引用
4.幻影參考
這是引用對象的默認(rèn)類型/類。任何具有活動強(qiáng)引用的對象都沒有資格進(jìn)行垃圾回收。只有當(dāng)被強(qiáng)引用的變量指向 null 時(shí),對象才會被垃圾回收。
MyClass obj = new MyClass();
這里的 'obj' 對象是對新創(chuàng)建的 MyClass 實(shí)例的強(qiáng)引用,當(dāng)前 obj 是活動對象,因此不能被垃圾收集。
對象=空;
//'obj' 對象不再引用實(shí)例。
所以'MyClass 類型對象現(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;
}
}
弱引用對象不是引用對象的默認(rèn)類型/類,在使用它們時(shí)應(yīng)明確指定。
這種類型的引用在 WeakHashMap 中用于引用條目對象。
如果 JVM 檢測到一個只有弱引用的對象(即沒有強(qiáng)或軟引用鏈接到任何對象對象),該對象將被標(biāo)記為垃圾回收。
要創(chuàng)建此類引用,使用java.lang.ref.WeakReference類。
這些引用在實(shí)時(shí)應(yīng)用程序中使用,同時(shí)建立一個 DBConnection,當(dāng)使用數(shù)據(jù)庫的應(yīng)用程序關(guān)閉時(shí),垃圾收集器可能會清理該 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
可以招募兩種不同級別的弱點(diǎn):軟弱點(diǎn)和幻影弱點(diǎn)
在軟引用中,即使對象可以進(jìn)行垃圾回收,也不會被垃圾回收,直到 JVM 嚴(yán)重需要內(nèi)存。當(dāng) JVM 內(nèi)存不足時(shí),對象會從內(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
幻影引用所引用的對象符合垃圾回收條件。但是,在將它們從內(nèi)存中刪除之前,JVM 會將它們放入一個名為 'reference queue' 的隊(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í)錯誤:
線程“主”java.lang.NullPointerException 中的異常
在 Example.main(Example.java:31)
輸出:
GeeksforGeeks
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743