更新時間:2022-08-25 09:51:22 來源:動力節點 瀏覽972次
在 Java 中,有四種類型的引用在它們被垃圾收集的方式上有所不同。
強引用
弱引用
軟引用
幻影引用
先決條件:垃圾收集
這是引用對象的默認類型/類。任何具有活動強引用的對象都沒有資格進行垃圾回收。只有當被強引用的變量指向 null 時,對象才會被垃圾回收。
MyClass obj = new MyClass();
這里的 'obj' 對象是對新創建的 MyClass 實例的強引用,當前 obj 是活動對象,因此不能被垃圾收集。
對象=空;
//'obj' 對象不再引用實例。
所以'MyClass 類型對象現在可用于垃圾收集。
// 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;
}
}
弱引用對象不是引用對象的默認類型/類,在使用它們時應明確指定。
這種類型的引用在 WeakHashMap 中用于引用條目對象。
如果 JVM 檢測到一個只有弱引用的對象(即沒有強或軟引用鏈接到任何對象對象),該對象將被標記為垃圾回收。
要創建此類引用,使用java.lang.ref.WeakReference類。
這些引用在實時應用程序中使用,同時建立一個 DBConnection,當使用數據庫的應用程序關閉時,垃圾收集器可能會清理該 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
可以招募兩種不同級別的弱點:軟弱點和幻影弱點
在軟引用中,即使對象可以進行垃圾回收,也不會被垃圾回收,直到 JVM 嚴重需要內存。當 JVM 內存不足時,對象會從內存中清除。創建這樣的引用使用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
幻影引用所引用的對象符合垃圾回收條件。但是,在將它們從內存中刪除之前,JVM 會將它們放入一個名為 'reference queue' 的隊列中。在對它們調用 finalize() 方法后將它們放入引用隊列中。使用java.lang.ref.PhantomReference類創建此類引用。
//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();
}
}
運行時錯誤:
線程“主”java.lang.NullPointerException 中的異常
在 Example.main(Example.java:31)
輸出:
GeeksforGeeks
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習