可达性分析算法中,哪些可以作为 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中的所有对象。

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