背景:

之前面试阿里支付宝,被问到常见的GC Root 是什么?
当时自己支支吾吾,明明自己看过深入理解Java 虚拟机这本书,但是就是回答不上来。

后来自己工作中,遇到内存泄漏问题。我百度,下载了MAT,去分析内存泄漏。问题很简答就处理完了。
但是,我只是使用了MAT 工具。 对于GC Root 都有什么还是什么都不懂,内存泄漏问题也是尝试着去解决。
并没有特别有信心。

Java 垃圾回收机制

Java 垃圾回收机制,一般使用GC Root 可达性分析算法,去判断一个对象是否可以被回收。
可达性,也就是这个对象的引用连是否和GC Root 相连。那么关键就是,什么是GC Root?
搜索了很多博客,都讲得不清楚。后来果断去google 搜索。果然结果令人满意:

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.

以上,就是一些比较常见的GC Root 对象。其实也好理解。

Java Local  本地变量表,如果执行到某个方法的时候,进行了垃圾回收,那么方法里面的局部变量引用的对象是不能被回收的。
不然怎么继续执行程序? 同样,JNI Local 也是一样的。
Busy Monitor 正在使用的锁对象,肯定不能被回收,不然影响程序运行。
Thread Block 线程里面的变量,一个活着的线程里面的对象肯定不能被回收。
System Class  被系统classloader 加载的类,引用的对象肯定不能回收。比如:rt.jar 里面的一些对象。
总之一句话,GC Root 对象一定是影响程序运行的对象。

分析具体问题:

用MAT 打开.hprof 文件,点击Leak Suspects:

java执行groovy脚本性能 java gcroots_Java


如下图,它会分析出几个问题,我们点开一个看下:

java执行groovy脚本性能 java gcroots_Java_02

按照下图的方法,我们查看下这个Fragmet 到GC Root 的引用链,去掉软弱引用,因为这些不影响垃圾回收。

java执行groovy脚本性能 java gcroots_eclipse_03

我们看到,这个Fragment 被 HightLighter 的 mInserAdListener 引用,HightLighter 这个类又被Native Stack 引用,导致Fragment泄漏。

java执行groovy脚本性能 java gcroots_Java_04

解决方法:

因为当gc 内存回收的时候,会去回收HightLighter 这个类,但是这个了类被Native 引用,所以回收不了。但是这个类还引用了一个Fragment,其实这个Fragment onDestory 之后,不应该被这个HightLighter 引用了。
那么:
当Fragment 关闭的时候,这个HightLighter 类的 成员变量mInserAdListener 不应该再引用Fragment 了。
把这个mInserAdListener 置为空,不引用Fragment 就行了。

参考资料:

推荐阅读 https://help.eclipse.org/oxygen/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fgcroots.html

英文的:
https://www.dynatrace.com/resources/ebooks/javabook/how-garbage-collection-works/

eclipse 的,比较好:
https://help.eclipse.org/oxygen/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fgcroots.html

ibm 的,比较好:
https://www.ibm.com/support/knowledgecenter/en/SS3KLZ/com.ibm.java.diagnostics.memory.analyzer.doc/gcroots.html

MAT 使用官方文档,写的超级好,比博客里面写得好:
https://help.eclipse.org/oxygen/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fgcroots.html

比较不错的,说了大概
https://yq.aliyun.com/articles/91017?utm_campaign=wenzhang&utm_medium=article&utm_source=QQ-qun&2017531&utm_content=m_22117


总结:

1.我觉得,应该抓住工作中每一个学习新知识的机会。我觉得是,不应该只是简单的解决问题。而应该抓住解决问题 带来的弥补自己知识盲点的机会,提升自己。