GC:垃圾回收
Q1:为什么要垃圾回收?
为了腾出内存给其他对象。
Q2:回收谁?(谁是垃圾?)
堆内存中,没有被GC root直接和间接引用的对象,就可能被回收。
Q3:谁是GC root?
A garbage collection root is an object that is accessible from outside the heap.
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。GC roots代表了一个集合,表示了一群活跃的引用。这个地方需要先了解JVM中的内存区域,然后才比较好理解。
包括但不限于:当前线程栈帧指向的引用;JNI的局部和全局变量引用的对象;被系统加载的类;用于同步监控的对象(synchronized、wait、notify);JVM持有的一些特殊对象;static修饰的对象;
借用知乎RednaxelaFX的回答总结:
Q4:如何找到垃圾?
从GC root开始,没有在引用链上面的对象就是垃圾。
Q5:垃圾回收算法有哪些?
Mark-Sweep:标记内存中的存活对象和垃圾对象,然后把垃圾直接清除,回收内存;缺点是碎片化。
Copying:将内存对半分成两份,每次把存活对象挨个复制到另一半;优势是无碎片,缺点是占空间。
Mark-Compact:标记内存中存活对象,然后把它们压缩在一起;优势是无碎片,缺点是慢。
Q6:什么是GC分代?
内存区域,来自马士兵Java视频:
JVM堆内存区域划分:Eden、两个Survivor、Tenured;Eden和2个Survivor合称新生代,大小默认是8:1:1,tenured老年代。
Q7:GC的流程和对象的年龄
GC分为两种,一种是Minor GC,只针对新生代;一种是Major GC/ Full GC,针对整个堆内存。
关于GC的流程,有一个传说:
《GC宇宙》
- 所有对象刚出生的时候在伊甸园(Eden)。
- 当伊甸园住满,就轮到上帝发大水了(JVM触发新生代垃圾回收:Minor GC),这些新生儿就夭折了一大半(清除了占绝大多数的无用的对象),苟活的这部分对象就被集中到了其中一个幸存者营地(Survivor1)。
- 经过这轮清洗(Minor GC),幸存者们都长大了一岁;然而祸不单行,又一次的清洗(Minor GC)到来了……
- 经过两轮清洗的幸存者数量变得更少了。他们和这段时间出生的幸存者们一起到了另一个幸存者营地(Survivor2),并且年龄又变大了一岁,现在最大的幸存者已经两岁了。
- 在经历了15轮浩劫之后,15岁的幸存者只剩寥寥数人。当他们睁开眼,本以为会继续和师弟师妹们渡劫,却来到了一片从未涉足之地。原来,当对象经历了15次渡劫,就会飞升到仙界(tenured)。
- 到了仙界之后,对象们就不再会被洪水淹了(老年代的对象不受Minor GC影响)。虽说摆脱了自身劫数的影响,然而仙界也不是无限大的。当成仙的对象到了一定的数量,就会降临终极大劫数——诸神黄昏(老年代空间不够用了,就会触发Full GC)。这时,即使是仙对象,也可能灰飞烟灭。
- 番外
A:据说很多对象成仙之后都懈怠了修为,无法抵挡诸神黄昏,最后都死在了35岁呢。
B:不是吧,还是有很多很厉害的。比如996岁的教师,251岁的菊华,都是上古大能呢!
A:是啊,终究还是得靠自己的努力。啊,不好,洪水来了!
……
韭菜一茬接一茬,后浪一浪接一浪,而GC的故事,仍将继续……
参考链接
Java中什么样的对象才能作为gc root,gc roots有哪些呢?
建议收藏丨马士兵亲授:JVM调优实战丨Jvm垃圾回收原理丨垃圾回收算法丨PB级大数据项目实战
Major GC和Full GC的区别是什么?触发条件呢? - RednaxelaFX的回答 - 知乎
T