JVM | garbage collection
Java garbage collection concept
- How to decide what is garbage objects, reference counting, root search algorithm
- What is GC Roots
- Reference JVM stack
- Methods district static variable references
- Reference the JNI (i.e., native method) of
- The method area (permanent behalf) GC occurs right, which objects will be recovered?
- The method of garbage collection zone to recover primarily of two parts: waste constants and useless class
- Will be recycled under the constant pool no object is referenced elsewhere in the GC, it is necessary: Waste Constant
- Useless categories: useless class must meet three conditions
- All the instance objects are recovered, the JVM class does not exist any instance
- Load class ClassLoader has been recovered
- Class Class object does not have any references, there is no place to refer to the class by reflection
- Virtual machine may be recovered based useless satisfy the above conditions, by
-Xnoclassgc
closing the recovery of class (expansion read: the JVM Parameter Description ) - In heavy use of reflection, dynamic proxies, custom ClassLoader scene, there will be a large number of classes are created, you need to have JVM uninstall feature class.
- When minor GC and full GC happens
- minorGC opportunity:
- When the JVM not to create a new object allocation space, will trigger minor GC, such as the newly created object is larger than the remaining space Eden District
- Will be called once before the full GC minor GC
- If you set the
-XX:+CMSScavengeBeforeRemark
parameters, CMS will perform a minor GC before re-mark
- fullGC opportunity:
- minor GC every time the object was promoted from the new generation to the old age of the average size of> the remaining space of old age
- After minor GC survival of the new generation of the total size of the object> the remaining space of old age
- Large objects directly into the old era, then the size of the object exceeds the remaining space years old
- By
-XX:PretenureSizeThreshold=xxx
can set the standard large object, more than this value, it will be put directly to the old time, pay attention this time is not a minor GC trigger - PretenureSizeThreshold parameters only for Serial and ParNew two collector effective, Parallel Scavenge collector does not know this parameter, Parallel Scavenge collector generally does not need to set. If you encounter a situation you must use this parameter, consider a collector combination ParNew plus the CMS
- By
- Permanent band ( PermGen JDK7 ) or meta-space ( the Metadata jdk8 ) will be insufficient to trigger full GC
- And with permanent element spaces are Hotspot implementation of the method area.
- Meta JDK8 default maximum space to 21M, can
-XX:MetaspaceSize=xxM
set the size of the space element - View JVM initialization parameter default values:
java -XX:+PrintFlagsInitial
- minorGC opportunity:
Garbage collector
Safety and security of the region point
我对于安全点的理解,把GC的过程看成街道消毒的过程,用户线程就是在街上跑步前进的人,安全点就是街道上每隔一段距离设置的一个防空洞,街上的人会有三种状态:跑步(running),在防空洞等待消毒完成(block),在街外面等人(in native),每个人状态修改时,必须将自己的状态写在一个本子上(serialization page内存).消毒的时候,必须确保所有人都已经躲进地下的防空洞中了或者在街外等人.这要怎么做呢,首先我准备消毒的时候,会设置一个信号灯(sync_state),在街上跑步的人每路过一个防空洞,都要看一下这个预备消毒的信号灯有没有亮,如果亮了,就躲进防空洞里面,停止跑步,并修改自己的状态为阻塞(block),等消毒完成后在出来继续跑.在消毒的过程中,街道办事处就不允许人修改自己的状态了(设置serialization page只读).
安全区域就是一开始我在防空洞里面睡觉(线程调用sleep),或者我到街外面等人去了(等待IO),在睡觉或者等待的时候,我会把我的状态修改好(block或者in native),而且在我睡醒或者等到人了之后,在回到街道之前,我要去修改我的状态,如果当前街道还在消毒,办事处不会让我改状态的(serialization page只读),只有等到消毒完成,我才可以重新回到街道.
-
安全点的位置
- 方法返回前
- 调用方法之后
- 抛出异常的位置
- 循环的末尾
- 如果for循环中有明确的循环计数器变量,而且该变量有明确的起始值、终止值、步进长度的循环,它有可能被优化为循环末尾没有safepoint,这种循环被称为'counted loop'于是如果这个循环的循环次数很多、循环体里又不调用别的方法或者是调用了方法但被内联进来了,就有可能会导致进入safepoint非常耗时。
- 解决办法: 把单层循环拆成等价的双重嵌套循环,这样其中一层循环末尾的safepoint就可能会留下来,减少进入safepoint的等待时间。
-
为啥要在这四个地方设置安全点
JVM要等所有应用线程进入到安全点后才会分派GC任务,如果有线程一直没有进入到安全点,就会导致GC的停顿时间延长. -
安全点应用实战
- 生产环境推荐使用
-XX:+PrintGCApplicationStoppedTime
打印JVM停顿时间,如果GC日志前面有较大的停顿,要就要考虑是不是代码里有较大的循环操作. - 在测试环境开启安全点统计日志
-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=/dev/shm/vm.log -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1
- 生产环境推荐使用
-
看看大神是怎么解决问题的 无法进入安全点导致GC停顿时间过长问题
-
推荐 JVM安全点介绍及实战
-
推荐 安全点图解
-
推荐 CMS垃圾收集器详细介绍
ID[1]
12466925 ↩