JVM summary (6) G1 garbage collector in JVM

Let's first review the mainstream Java garbage collector (HotSpot JVM). This article discusses heap garbage collection.

The heap is broken down into three smaller parts. Specifically, it is divided into: new generation, old generation, persistent generation.

  1. Most of the newly generated objects are placed in the Eden area. When the Eden area is full, the JVM will trigger Young GC because it cannot apply for memory, and perform garbage collection in the Eden area + the Survivor area with objects (set as S0 area). Copy the surviving objects to an empty Survivor (S1) using the replication algorithm. At this time, the Eden area is emptied, and the other Survivor S0 is also empty. The next time Young GC is triggered to recycle Eden+S0, the surviving objects are copied to S1. Cenozoic garbage collection is simple, crude, and efficient.
  2. If it is found that the Survivor area is full, these objects are copied to the old area or the Survivor is not full but some objects are old enough, and also copied to the Old area (each Young GC will make the Survivor area Survivor object value +1 until the threshold). 3. Garbage collection (Young GC) is also performed in the Old area. A Major GC is accompanied by at least one Young GC, which is generally more than ten times slower than the Young GC.
  3. If the JVM cannot apply for memory in the Old area, it will perform a Full GC. The old area generally uses the Concurrent-Mark-Sweep strategy to reclaim memory.

Summary: The Java garbage collector is an "adaptive, generational, stop-copy, mark-sweep" garbage collector.

shortcoming:

  1. STW (Stop-The-World) will appear during the GC process. If there are too many objects in the Old area, STW will consume a lot of time.
  2. The CMS collector is sensitive to CPU resources.
  3. The CMS collector cannot handle floating garbage, and a "Concurrent Mode Failure" may fail, resulting in another Full GC.
  4. CMS causes memory fragmentation issues.

G1 collector

In G1, the heap is divided into a number of consecutive regions (regions). Each area is equal in size, between 1M and 32M. The JVM supports up to 2000 regions, and it can be estimated that the maximum memory that G1 can support is 2000*32M=62.5G. The size of the region is determined when the JVM is initialized, and can also be set with -XX:G1HeapReginSize.

There is no physical Yong (Eden/Survivor)/Old Generation in G1, they are logical, using some non-consecutive regions (Region).

Cenozoic collection

The collection of the new generation of G1 is similar to that of ParNew. When the occupation of the new generation reaches a certain proportion, the collection starts.

The circled green part is the Cenozoic region. Objects that survived Young GC are copied to one or more free regions. These filled regions will be the new Cenozoic; The age (after escaping a Young GC age increases by 1) has reached a certain threshold (ParNew defaults to 15), and is copied to the area of ​​the old generation.

The recycling process is paused (STW, Stop-The-Word); after the recycling is completed, the sizes of Eden and Survivor are adjusted according to the statistics of Young GC, which helps to rationally utilize memory and improve recycling efficiency.

During the recycling process, multiple recycling threads collect concurrently.

old age collection

Similar to CMS, the G1 collector will have a short pause when collecting old generation objects.

  1. In the marking phase, the initial mark (Initial-Mark) is first, this phase is stopped (Stop the World Event), and a normal Mintor GC will be triggered. Corresponding to GC log:GC pause (young) (inital-mark)
  2. Root Region Scanning, the survivor area will be reclaimed (surviving to the old age) during the program running. This process must be completed before the young GC.
  3. Concurrent Marking, doing concurrent marking (and application concurrent execution) in the entire heap, this process may be interrupted by young GC. In the concurrent marking phase, if all objects in the area object are found to be garbage, the area will be reclaimed immediately (X in the figure). At the same time, during the concurrent marking process, the object activity of each region (the proportion of surviving objects in the region) is calculated.
  4. Remark, 再标记,会有短暂停顿(STW)。再标记阶段是用来收集 并发标记阶段 产生新的垃圾(并发阶段和应用程序一同运行);G1中采用了比CMS更快的初始快照算法:snapshot-at-the-beginning (SATB)。
  5. Copy/Clean up,多线程清除失活对象,会有STW。G1将回收区域的存活对象拷贝到新区域,清除Remember Sets,并发清空回收区域并把它返回到空闲区域链表中。
  6. 复制/清除过程后。回收区域的活性对象已经被集中回收到深蓝色和深绿色区域。

关于Remembered Set概念:G1收集器中,Region之间的对象引用以及其他收集器中的新生代和老年代之间的对象引用是使用Remembered Set来避免扫描全堆。G1中每个Region都有一个与之对应的Remembered Set,虚拟机发现程序对Reference类型数据进行写操作时,会产生一个Write Barrier暂时中断写操作,检查Reference引用的对象是否处于不同的Region之间(在分代中例子中就是检查是否老年代中的对象引用了新生代的对象),如果是便通过CardTable把相关引用信息记录到被引用对象所属的Region的Remembered Set中。当内存回收时,在GC根节点的枚举范围加入Remembered Set即可保证不对全局堆扫描也不会有遗漏。

Although G1 retains the concept of generation in CMS, generation is no longer a physically continuous area, but a logical concept. In the marking process, the object activity of each area is calculated. When recycling, you can select the area with lower activity to collect according to the pause time set by the user, which can not only ensure garbage collection, but also ensure the pause time, and It also doesn't degrade the throughput too much. The application of the new algorithm in the Remark phase, as well as the compression in the collection process, make up for the shortcomings of the CMS. Quoting a sentence from Oracle's official website: "G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS)".

refer to:

  1. Memory Management in the Java HotSpot™ Virtual Machine
  2. Getting Started with the G1 Garbage Collector
  3. Garbage-first garbage collector tuning
  4. Deep Understanding of Java Virtual Machine: JVM Advanced Features and Best Practices
Original blog: http://www.importnew.com/15311.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693410&siteId=291194637