可達性分析算法中,哪些可以作爲 root?

java垃圾回收的可達性分析算法, 列舉可以作爲root的對象: 

由於網上的博客回答都不全, 找了份來自help.eclipse.org的, 先上原文與連接供大佬參考, 後邊掛上垃圾翻譯, 不接受關於翻譯的質疑! (認真臉)

https://help.eclipse.org/luna/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:

System Class

Class loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.* .

JNI Local

Local variable in native code, such as user defined JNI code or JVM internal code.

JNI Global

Global variable in native code, such as user defined JNI code or JVM internal code.

Thread Block

Object referred to from a currently active thread block.

Thread

A started, but not stopped, thread.

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.

Java Local

Local variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.

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.

Finalizable

An object which is in a queue awaiting its finalizer to be run.

Unfinalized

An object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.

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.

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.

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.

 垃圾回收的root是可能來自於堆外的對象,下面是可能讓對象作爲回收root的原因:

  • 系統類

    由bootstrap/system類加載器加載的類。例如,所有來自於rt.jar的類, 比如java.util.*

  • 本地JNI

    Native代碼中的本地變量,如用戶定義的JNI代碼或JVM內部代碼。

  • JNI全局變量

    Native代碼中的全局變量,如用戶定義的JNI代碼或JVM內部代碼。

  • 線程塊

    從當前活動的線程塊引用的對象。

  • 線程對象
    一個已經開始運行且沒停止的線程對象

  • 佔用監視器 ( Busy Monitor ) 
    調用了wait()或notify()或已同步的所有線程對象。例如,通過調用synchronized(object)或進入synchronized方法。靜態方法表示類,非靜態方法表示對象。

  • JAVA本地變量
    例如,仍在線程堆棧中的輸入參數或方法的本地創建對象。

  • 本地方法棧

    Native代碼中的輸入或輸出參數,如用戶定義的JNI代碼或JVM內部代碼。通常情況下,許多方法都有Native部分,並且作爲方法參數處理的對象成爲GC根。例如,用於文件/網絡I/O方法或反射的參數。

  • Finalizable

    等待Finalizable運行隊列中的對象
  • Unfinalized

    具有finalize方法, 但還沒被finalized也沒有在Finalizable隊列的對象

  • 不可達對象
    從任何其他root都無法訪問的對象,但已被MAT標記爲ROOT,以保留不回收的對象。

  • Java棧幀
    Only generated when the dump is parsed with the preference set to treat Java stack frames as objects.
    (翻譯不出來...)

  • 類型未知的其他可作爲root的對象

    root類型未知的對象。有些dumps(如IBM可移植堆轉儲文件, IBM Portable Heap Dump files)不包含root 信息。對於這些dumps ,MAT解析器將無法從任何其他root訪問的對象標記爲該類型的root。這樣可以確保MAT保留dumps中的所有對象。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章