JVM learning (7) -G1 garbage collector Introduction

A. Brief

Official Description of Document:

The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. The G1 garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is designed for applications that:

Can operate concurrently with applications threads like the CMS collector.
Compact free space without lengthy GC induced pause times.
Need more predictable GC pause durations.
Do not want to sacrifice a lot of throughput performance.
Do not require a much larger Java heap.

G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

As can be seen, G1 garbage collector that is Garbage-First Garbage Collector, is used as a substitute for long-term support CMS garbage collector after JDK7, it is to look at the advantages:

  1. Concurrent multi-core processor machine
  2. With respect to the CMS can reduce space debris and garbage clean-up time
  3. Can make garbage time becomes predictable
  4. It will not cause a significant loss of throughput
  5. It does not require a larger Java heap

G1 relative to the CMS has the following advantages:

  1. It will not have too much memory space debris
  2. Region area instead of using a new era of divided, old age
  3. CMS's only for the old recycling, and G1 of the entire space for recycling to garbage collection as the top priority
  4. You can set predictable garbage collection time

G1 garbage collector 9 is provided in order to JDK default garbage collector. In the low-level JDK version, you can: -XX:+UseG1GCparameter to enable G1 garbage collector.

II. Garbage collection area change

In general, the older the garbage collector such as Serial, ParNew, CMS and other garbage collector garbage collection process, will be divided into a new generation of Java heap, old and permanent generations it's three regions and were subjected to recovery:
Here Insert Picture Description
but G1 garbage collector uses a different form of zoning, it would be a Java heap memory into an equally large continuous space, and each identified space are different colors and letters become, for example, E (Eden), O (Old), S (Servivor) and the like:
Here Insert Picture Description

III. How to ensure the completion of garbage collection within the predicted time

G1 garbage collection model established a predictable, it maintains a list of recovery value by tracking the recovery value of each region and how much free space size, according to the priority list to recover lost the largest regional recovery value Region

G1 uses a global identifier to indicate which areas need to be recovered, the region range when a large amount of free space for the region within this range will be immediately recovered for recycling, so that memory fragmentation rate decreased, and reached the goal G1. Pause time garbage collection process is controllable and so on

IV. Garbage collection process

Here Insert Picture Description

Similar garbage collection process and CMS garbage collector G1 garbage collector can be divided into 初始标记, 兵法标记, 重新标记and 筛选回收these four stages, which 并发标记with the 筛选回收accompanying user threads together, no stw.

1. Initial marker

stw, marking GC ROOTS not related to the object. It takes very little time.

2. concurrent mark

Performed simultaneously with the user thread, the initial mark in the acquired GC ROOTS object tracking, found objects and recycled objects do not need to need to be recovered. With the longest time.

3. Re-mark

STW, this step is to mark in a concurrent mark to the new user thread recycled objects produced. With a shorter time.

4. Filter Recycling

At the same time executing user threads, regional recoverable value lists and filter through a comprehensive analysis of the predicted recovery time for the region's recovery and recycling out, using the copy algorithm. Off the user with the predicted recovery time. Here is the biggest consuming garbage collection time set by the user, can -XX:MaxGCPauseMillis=parseMIllsbe set.

Thanks

  1. https://www.breakyizhan.com/javamianshiti/2861.html
  2. https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html#t2
Published 309 original articles · won praise 205 · Views 300,000 +

Guess you like

Origin blog.csdn.net/pbrlovejava/article/details/103746330