GC如何判断对象是否死亡

GC如何判断对象是否死亡

引用计数法

给对象中添加一个引用计数器,每当有一个地方引用它,计数器就加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)保留转储中的所有对象。

猜你喜欢

转载自blog.csdn.net/Davids_/article/details/106362359
今日推荐