1.虚拟机栈(本地变量表)引用的对象
2.方法区静态属性引用的对象
3.方法区常量引用的对象
4.本地方法栈JNI(一般指naive方法)中引用的对象
常说的GC(Garbage Collector) roots,特指的是垃圾收集器(Garbage Collector)的对象,GC会收集那些不是GC roots且没有被GC roots引用的对象。
一个对象可以属于多个root,GC root有几下种:
- Class - 由系统类加载器(system class loader)加载的对象,这些类是不能够被回收的,他们可以以静态字段的方式保存持有其它对象。我们需要注意的一点就是,通过用户自定义的类加载器加载的类,除非相应的
java.lang.Class
实例以其它的某种(或多种)方式成为roots,否则它们并不是roots,. - Thread - 活着的线程
- Stack Local - Java方法的local变量或参数
- JNI Local - JNI方法的local变量或参数
- JNI Global - 全局JNI引用
- Monitor Used - 用于同步的监控对象
- Held by JVM - 用于JVM特殊目的由GC保留的对象,但实际上这个与JVM的实现是有关的。可能已知的一些类型是:系统类加载器、一些JVM知道的重要的异常类、一些用于处理异常的预分配对象以及一些自定义的类加载器等。然而,JVM并没有为这些对象提供其它的信息,因此就只有留给分析分员去确定哪些是属于"JVM持有"的了。
以下是一张由Java Profiler的标示出哪些是GC roots的示例图:
作者:RednaxelaFX
链接:https://www.zhihu.com/question/53613423/answer/135743258来源:知乎著作权归作者所有,转载请联系作者获得授权。
之前看深入理解JVM这本书,对里面的GC ROOT的真实含义不是太清楚,网上查了一大堆资料都没有说的很清楚,下面这是从知乎大神上看到的,这里面记录一下,和大家一起学习
所谓“GC roots”,或者说tracing GC的“根集合”,就是一组必须活跃的引用。 例如说,这些引用可能包括:
- 所有Java线程当前活跃的栈帧里指向GC堆里的对象的引用;换句话说,当前所有正在被调用的方法的引用类型的参数/局部变量/临时值。
- VM的一些静态数据结构里指向GC堆里的对象的引用,例如说HotSpot VM里的Universe里有很多这样的引用。
- JNI handles,包括global handles和local handles
- (看情况)所有当前被加载的Java类
- (看情况)Java类的引用类型静态变量
- (看情况)Java类的运行时常量池里的引用类型常量(String或Class类型)
- (看情况)String常量池(StringTable)里的引用
Garbage Collection RootsA garbage collection root is an object that is accessible from outside the heap. The following reasons make an object a GC root:System ClassClass loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.* .JNI LocalLocal variable in native code, such as user defined JNI code or JVM internal code.JNI GlobalGlobal variable in native code, such as user defined JNI code or JVM internal code.Thread BlockObject referred to from a currently active thread block.ThreadA started, but not stopped, thread.Busy MonitorEverything 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 LocalLocal variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.Native StackIn 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.FinalizableAn object which is in a queue awaiting its finalizer to be run.UnfinalizedAn object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.UnreachableAn 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 FrameA Java stack frame, holding local variables. Only generated when the dump is parsed with the preference set to treat Java stack frames as objects.UnknownAn 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作者:秦汉邮侠链接:https://www.jianshu.com/p/f4ff9fcc0759來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
参考:
https://www.jianshu.com/p/f4ff9fcc0759
posted on 2019-01-08 19:17 阅读( ...) 评论( ...)