The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1.

2025/06/2814:38:36 technology 1605

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

JVM garbage collector is a must-have content. This article focuses on the G1 collector @mikechen

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

G1 collector overview

HotSpot team has been working hard to efficiently collect and reduce pauses (STW: Stop The World), and has also contributed a series of excellent garbage collectors, including serial Serial collectors, parallel collector Parallerl collectors, CMS concurrent collectors, and even today's G1.

G1 (Garbage First) garbage collector focuses on the minimum delayed garbage collector, and is also suitable for garbage collection of large-sized heap memory. The official also recommends using G1 instead of choosing CMS.

1. The biggest feature of G1 collector

  • G1's biggest feature is the introduction of partitioning ideas, which weakens the concept of generational division.
  • rationally utilizes the resources of garbage collection in each cycle, solving many defects of other collectors and even CMS.

2.G1 Compared with CMS, the improvement of

  • algorithm: G1 is based on the tag-organization algorithm, and will not produce space fragmentation. When allocating large objects, it will not fail to obtain continuous space and trigger FULL GC in advance.
  • Pause time is controllable: G1 can control garbage collection time by setting the expected pause time (Pause Time).
  • Parallel and Concurrency: G1 can make full use of CPU, and the hardware advantages in multi-core environments to shorten the pause time of stop the world.

3. The difference between CMS and G1

  • CMS, the heap is divided into PermGen, YoungGen, and OldGen; and YoungGen is divided into two survivo areas. In G1, the heap is divided into regions evenly. In each region, although the concept of new and old generations is also retained, the collector is collected in units of the entire region.
  • G1 will immediately merge free memory after reclaiming memory, while CMS is done by default at STW (stop the world).
  • G1 will be used in Young GC, while CMS can only be used in Area O.

4. Application scenarios of G1 collector

G1 garbage collection algorithm is mainly used in services with large memory of multi-CPUs. While meeting high throughput, it meets the pause time during garbage collection as much as possible.
For now, CMS is still the preferred GC strategy by default. G1 may be more suitable in the following scenarios:

  • Applications with multi-core CPU on the server and large JVM memory occupancy (at least greater than 4G)
  • application will generate a large amount of memory fragments during operation and require frequent compression of space
  • wants a more controllable and predictable GC pause cycle to prevent application avalanche under high concurrency

G1's heap memory algorithm

1. The JVM memory model before G1

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

  • New Generation: Eden (eden) space) + 2 surviving areas
  • Older
  • Perm space: JDK1.8
  • metaspace: JDK1.8 replaced the persistent generation

2.G1 collector memory model

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

1)G1 heap memory structure
heap memory will be divided into many fixed-sized areas (regions), each of which is a continuous range of virtual memory. The size of a region in
heap memory can be specified by the -XX:G1HeapRegionSize parameter, the size interval is at least 1M and the maximum is 32M, in short, it is a power of 2.

divides the heap memory equally according to 2048 copies by default.

2)G1 heap memory allocation
Each Region is marked E, S, O and H, and these regions are logically mapped to Eden, Survivor and Old Age.
Surviving objects are transferred from one area (i.e. copy or move) to another area. Areas are designed to collect garbage in parallel and may pause all application threads.
As shown in the figure above, the area can be assigned to Eden, Survivor and the Elderly. In addition, there is a fourth type, called the Humongous Region.The Humongous area is designed for storing objects that are more than 50% of the standard region size, and it is used to store giant objects specifically. If a H zone cannot install a giant object, then G1 will look for consecutive H partitions to store. In order to find continuous H zones, you sometimes have to start Full GC.

G1 Recycling Process

When performing garbage collection, G1 runs in a similar way to the CMS collector.

1. The stage of G1 collector is divided into the following steps:

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews


1) The first stage of G1 execution: Initial Marking
This stage is STW (Stop the World). All application threads will be paused and objects that can be directly accessible from GC Root are marked.

2) The second stage of G1 execution: the concurrent tag
starts with GC Roots to perform accessibility analysis on the objects in the heap and find the surviving objects, which takes a long time. After the concurrent marking is completed, the final marking stage is started.

3) and the final marking (mark those objects that change in the concurrent marking stage will be recycled)

4) filtering and recycling (firstly sort the recycling value and cost of each register, specify the recycling plan based on the expected GC pause time of the user, and recycle part of the Region)

Finally, two mode garbage recycling modes are provided in G1, Young GC and Mixed GC, both of which are Stop The World (STW).

G1's GC mode

1. YoungGC young generation collects

When allocating general objects (non-giant objects), YoungGC will be triggered once when all eden regions use reaches the maximum threshold and cannot apply for sufficient memory. Each time younggc recycles all Eden and Survivor areas and copies the survivor objects to the Old area and another part of the Survivor area. The recycling process of
YoungGC is as follows:

  • root scans, similar to CMS, Stop the world, scans GC Roots objects.
  • processes Dirty card, update RSet.
  • scan RSet, scan all old areas in the RSet to the scanned young area or survivor references.
  • Copy the scanned survivor2/old area
  • to process the reference queue, soft reference, weak reference, virtual reference

2.mixed gc

When more and more objects are promoted to the old region, in order to avoid the heap memory being exhausted, the virtual machine triggers a mixed garbage collector, i.e. mixed gc. This algorithm is not an old gc. In addition to recycling the entire young region, it will also recycle part of the old region. Note here: it is part of the old age, not the whole old age. Which old regions can be selected for collection, so that the time-consuming time of garbage collection can be controlled.
G1 does not have the concept of fullGC. When fullGC is needed, serialOldGC is called for full heap scanning (including eden, survivor, o, perm). Recommended use cases for

G1

G1 The first important feature of

G1 is to provide a solution for low GC delay and large memory GC for users' applications. This means the heap size is 6GB or larger, and the stable and predictable pause time will be less than 0.5 seconds.
If the application uses CMS or ParallelOld garbage collector with one or more of the following characteristics, it will be beneficial to switch to G1:

  • Full GC lasts too long or too frequent
  • Object allocation rate or young generation upgrades old generation
  • Unexpected long garbage collection time or compression pause (more than 0.5 to 1 second)

Note: If you are using CMS or ParallelOld collector and your application does not experience a long garbage collection pause, it is good to keep with your current collector, upgrading the JDK does not need to update the collector to G1.

or above!

Read more Architecture technology collection

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

The HotSpot team has been working hard to efficiently collect and reduce pauses, and has also contributed a series of excellent garbage collectors, including serial Serial collectors, Parallel collectors, CMS concurrent collectors, and even today's G1. - DayDayNews

Author: Chen Rui|mikechen mikechen's Internet architecture author

Original address: https://mikechen.cc/7126.html

This article was originally published by @mikechen on mikechen's Internet architecture. Reproduction is prohibited without permission.

technology Category Latest News