Java Virtual Machine knowledge [GC]

First, the garbage collection algorithm

  1. Mark - sweep algorithm
      first mark all need to be recovered objects, then all objects are marked uniform recycling. The algorithm efficiency is not high , and there is memory fragmentation problems.

  2. Replication algorithm
      memory capacity divided by two of equal size, which uses only one memory allocation, when this memory runs out, it will still all live objects copied to another block of memory, and the used memory space once cleared away. The algorithm can solve the problem of efficiency mark clear algorithm. However, because of the need to divide half the memory, a higher price.

  3. Mark - sorting algorithm
      marks all objects need to be recovered, so live objects move toward one end, then clean out the memory directly outside the terminal boundary. The algorithm can solve the problem of memory fragmentation mark sweep algorithm, as well as the object replication algorithm at high survival rate, multiple replication efficiency becomes low.

  4. Generational collection algorithm for
      the new generation, each has a large number of dead objects garbage collection, only a few survive, then you have to use the copy algorithm, so long as the pay copying costs a small amount of live objects to complete the collection;
      the old era, the object high survival rate, no extra space is allocated to guarantee him, you have to use markers or tags to organize clean-up algorithm;
      generational collection algorithm will heap space is divided into the old and the young generation yang years old, the young generation has been divided into zones and Eden Survivor area, Survivor region has been divided into zones with From to area. Default by 8: 1 division of Eden area and Survivor areas. Eden region is contiguous memory space, and therefore fast in Eden allocated memory area. HotSpot virtual machine using the pointer to speed collision and TLAB memory allocation Eden area and guarantee thread safety.
    Generational collection algorithm memory division

Execution flow generational collection algorithm

  1. New object in Eden priority allocation region;
  2. When the Eden area is full, it will trigger Minor GC, live objects are moved to Eden Survivor0, Eden is cleared;
  3. Eden area is full again and so on, trigger again live objects Minor GC, Eden and Survivor0 in will be copied to the Survivor1, S0 and Eden are cleared, then the next round of S0 and S1 switch roles, so on ad infinitum.
  4. After two Survivor areas switched several times (HotSpot virtual machine default 15 times), still alive objects are copied to the old era.

Minor GC : place in the new generation of GC, because Java objects have properties Chaosheng evening off, so Minor GC very frequently, usually recover relatively fast speed.
GC Major / Full GC : occurred in the old year's GC, appeared Full GC is often accompanied by at least one of the Minor GC (non-absolute, such as the Parallel Scavenge). Full GC's speed is generally slower than Minor GC more than 10 times, it is reasonable to set the size of the young generation and old age, to minimize the Full GC operation.

Minor GC trigger conditions:
  • Fires when the Eden area is full.
Full GC trigger conditions:
  • When calling System.gc, the system will recommend the implementation of Full GC, but not necessarily implemented.
  • Trigger years old when insufficient space.
  • The method area (on behalf of the permanent / dollar space) is triggered when the lack of space.
  • The average size of contiguous memory triggered by Minor GC after entering old age old age is greater than available.
  • Region of Eden, From time to copy the To Space Space zone area, the object Space available memory size is larger than the To, the object put dump year old, old age and available continuous memory size less than the trigger time of the object.

Second, memory allocation and recovery strategies

  • Objects priority in the allocation area in Eden
      when Eden area is not enough memory space is allocated, the virtual machine will launch
    a Minor GC.

  • Large objects directly into the old year
      a large number of contiguous memory Java objects, such as a long string and array will be assigned directly to the old era, and therefore should be avoided when writing programs.

  • Long-term survival of the object will enter the old year
      when the two Survivor areas switched several times (HotSpot virtual machine default 15 times), still alive objects are copied to the old era.

  • Age dynamic object is determined
      if the sum of all of the same age S0 space is larger than half the size of the object space S0, equal to or older than the age of the subject can directly enter years old, without reaching the threshold.

  • Space allocation guarantees
      before it happens Minor GC, virtual opportunity to check whether old's maximum available contiguous space is greater than all the objects in the new generation of total space, if this condition is met, then the Minor GC to ensure that it is safe. If not satisfied, the virtual opportunity to review whether to allow HandlePromotionFailure settings guarantee failure. If allowed, it will continue to check whether old's maximum available contiguous space larger than previous years old was promoted to the average size of the object, if greater than, will attempt to conduct a Minor GC, despite the Minor GC is risky; if less than, or HandlePromotionFailure settings do not allow risk, then it should be changed once a Full GC.

Third, the garbage collector

  1. Serial : single-threaded collector. == == replication algorithm
  2. ParNew : multithreaded version Serial collector. == == replication algorithm
  3. Scavenge Parallel : Similar ParNew collector, other collectors focus on the Stop The World shorten the time as much as possible, but more concerned about the Parallel collector system throughput , support adaptive strategies. == == replication algorithm
  4. Old Serial : Serial old collector's edition. == == mark Collation Algorithm
  5. Old Parallel : old's version of the Parallel Scavenge collector. == == mark Collation Algorithm
  6. The CMS : Concurrent Mark Sweep Collector is a kind of to get the shortest recovery time objectives pause collector. == == mark sweep algorithm

 The operation of the CMS process

  • The initial mark (Initial Mark) : marked objects inside old's survival, these objects are from GC roots or point directly, or survival of the object being pointed to the young generation. It will lead to STW, the fastest.

The initial mark

  • Concurrent mark (Concurrent Mark) : find all root from the stage start to traverse the entire old era, marking live objects. Slow, however, and the program is executed concurrently.

Concurrent mark

  • Relabeled (Remark, the Final) : As the previous concurrent mark, concurrent process, may not be able to catch up with the speed of an application to modify. So it is necessary to re-mark to mark the completion of the entire Older Generation survival object marking. It will lead to STW, fast.
  • Clear concurrent (Concurrent Sweep) : Concurrent clear target of death. Slow, however, and the program is executed concurrently.

Concurrent Clear

 CMS whole process

CMS collector

 The disadvantage of CMS

  • Of CPU resources are very sensitive. The default number of threads is recovered CMS (CPU number + 3) / 4.
  • 无法处理浮动垃圾,可能出现“Concurrent Mode Failure”失败而导致另一次Full GC的产生。CMS并发清理阶段用户线程还在运行,伴随程序运行自然有新的垃圾不断产生,这部分垃圾出现在标记过程之后,CMS无法在当次收集中处理掉它们,只好留到下次GC再清理。这部分垃圾就是浮动垃圾。因为垃圾收集阶段的用户线程还要运行,所以CMS不像其他收集器那样等老年代几乎填满了在收集,会预留一部分空间。-XX:CMSInitiatingOccupancyFraction可以设置触发的百分比。当预留的内存无法满足程序需要,就会出现出现“Concurrent Mode Failure”失败,此时,虚拟机启动后备方案:临时启用Serial Old。
  • 标记-清除算法的缺陷。易产生内存碎片。解决方法:通过参数配置,用于CMS在Full GC 时开启内存碎片的合并整理过程,内存整理过程无法并发,会导致STW时间变长,因此有另一个参数配置,用于设置执行多少次不压缩的Full GC后,执行压缩会Full GC。

7.G1:面向服务端应用。

 G1 的特点

  • 并发与并行:充分利用多CPU、多核环境的硬件优势,缩短STW。
  • 分代收集:保留分代概念,能独立管理整个GC堆。
  • 空间整理:基于“标记-整理“,局部(两个Region)上看基于“复制”算法。所以不会产生内存空间碎片。
  • 可预知的停顿:这是G1相对于CMS的另一大优势,能让使用者明确指定一个长度为M毫秒的时间片段内,消耗在垃圾收集上的时间不得超过N毫秒。

 G1 的运作过程

  • 初始标记(Initial Mark):类似CMS
  • 并发标记(Concurrent Mark):类似CMS
  • 最终标记(Remark):类似CMS
  • Filter Recovery (cleanp): At this stage the objects are copied from one or more regions into a single region, and the adjustment and free memory.

8. The a ZGC : introduction of the JDK. 11, claims to have lower latency garbage collector, using a colored pointer, barrier loading technology, the STW a control, the scanning can be realized only once garbage collection.

Fourth, the combination of garbage collector

parameter Features
-XX:+UseConcMarkSweepGC Automatically enabled -XX: + UseParNewGC
-XX:+UseParallelGC Automatically enabled -XX: + UseParallelOldGC. The default in Server mode.
-XX:+UseParallelOldGC Automatically enabled -XX: + UseParallelGC
-XX:+UseParNewGC JDK8 alone can not be enabled
-XX:+UseSerialGC Serial + Serial Old. The default in Client mode.
-XX:+UseG1GC Use G1 Garbage Collector

Reference: "in-depth understanding of the Java Virtual Machine (Second Edition)", "Java Virtual Machine Specification (Java SE version 8)", GC Algorithms: Implementations

Guess you like

Origin www.cnblogs.com/bigshark/p/11246545.html