Davids知識點筆記:GC如何判斷對象是否死亡(引用計數法、可達性分析算法)

GC如何判斷對象是否死亡

關注可以查看更多粉絲專享blog~

引用計數法

給對象中添加一個引用計數器,每當有一個地方引用它,計數器就加1,當引用失效,計數器就減1,任何時候計數器爲0的對象就是不可能再被使用的對象,則在GC時可以回收,無法解決循環依賴的問題。

可達性分析算法

該算法的基本思想就是通過一系列的成爲“GC Root”的對象作爲起點,從這些節點開始向下搜索,節點所走過的路徑稱爲引用鏈,當一個對象到GC Root沒有任何引用鏈相連的話,則證明此對象不可用,則在GC時可以回收。

什麼是GC Root對象

常說的GC(Garbage Collector) Roots,特指的是垃圾收集器(Garbage Collector)的對象,GC會收集那些不是GC Roots且沒有被GC Roots引用的對象。
有以下幾種:

GC Root help:
https://help.eclipse.org/2020-03/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fgcroots.html&cp=37_2_3

Garbage Collection Roots

A garbage collection root is an object that is accessible from outside the heap. The following reasons make an object a GC root:
GC Root是可以從堆外部訪問的對象。以下原因使對象成爲GC root:

1.System Class
Class loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.* .
由bootstrap/system加載的類。例如,rt.jar 和 java.util.*. 。

2.JNI Local
Local variable in native code, such as user defined JNI code or JVM internal code.
本地變量中的native方法,例如用戶定義的JNI代碼或JVM內部代碼。

3.JNI Global
Global variable in native code, such as user defined JNI code or JVM internal code.
全局變量中的native方法,例如用戶定義的JNI代碼或JVM內部代碼。

4.Thread Block
Object referred to from a currently active thread block.
當前活動的線程塊引用的對象。

5.Thread
A started, but not stopped, thread.
已啓動但未停止的線程。

6.Busy Monitor
Everything that has called wait() or notify() or that is synchronized. For example, by calling synchronized(Object) or by entering a synchronized method. Static method means class, non-static method means object.
調用了wait()或notify()或已同步的所有內容。例如,通過調用Synchronized(Object)或輸入同步方法。靜態方法表示類,非靜態方法表示對象。

7.Java Local
Local variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.
局部變量。例如,入參數或仍在線程堆棧中的方法的本地創建對象。

8.Native Stack
In or out parameters in native code, such as user defined JNI code or JVM internal code. This is often the case as many methods have native parts and the objects handled as method parameters become GC roots. For example, parameters used for file/network I/O methods or reflection.
本地方法中的入參或出參,例如,用戶定義的JNI代碼或JVM內部代碼。通常是這種情況,因爲許多方法具有本地方法,並且隨着方法參數而處理的對象成爲GC root。例如,用於文件/網絡I/O方法或反射的參數。

9.Finalizable
An object which is in a queue awaiting its finalizer to be run.
隊列中等待其回收器運行的對象。

10.Unfinalized
An object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.
具有finalize方法但尚未完成且尚未進入終結器隊列的對象。

11.Unreachable
An object which is unreachable from any other root, but has been marked as a root by MAT to retain objects which otherwise would not be included in the analysis.
無法從任何其他根目錄訪問的對象,但是已被MAT(Memory Analyzer Tool)標記爲根目錄以保留對象,否則該對象將不會包含在分析中。

12.Java Stack Frame
A Java stack frame, holding local variables. Only generated when the dump is parsed with the preference set to treat Java stack frames as objects.
一個Java堆棧框架,其中包含局部變量。僅在使用設置爲將Java堆棧幀視爲對象的首選項設置分析轉儲時才生成。

13.Unknown
An object of unknown root type. Some dumps, such as IBM Portable Heap Dump files, do not have root information. For these dumps the MAT parser marks objects which are have no inbound references or are unreachable from any other root as roots of this type. This ensures that MAT retains all the objects in the dump.
根類型未知的對象。某些轉儲(例如IBM Portable Heap Dump文件)沒有根信息。對於這些轉儲,MAT(Memory Analyzer Tool)解析器將沒有入站引用或無法從任何其他根訪問的對象標記爲該類型的根。這樣可以確保MAT(Memory Analyzer Tool)保留轉儲中的所有對象。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章