It contains 2,195 words and takes about eight minutes to read.

If the garbage collection algorithm belongs to the memory collection methodology, then the garbage collector discussed in this article belongs to the concrete implementation of memory collection.

Garbage collectors are implemented differently by different vendors (IBM, Oracle), and this article discusses the garbage collector used by Oracle’s HotSpot VIRTUAL machine.

Common garbage collector, as shown below:

  • Cenozoic recyclers: Serial, ParNew, Parallel Insane
  • Old collector: Serial Old, Parallel Old, CMS
  • Whole heap collector: G1

The garbage collectors connected to each other indicate that they can be used with each other.

New generation And old generation

The most common commercial garbage collectors use generational garbage collection.

The generational garbage collector divides memory into Young Generation and Tenured Generation, as shown below:

(image by fancydeepin)

By default, the memory ratio of the new generation to the old generation is 1:2. This value can be set by using -xx :NewRatio.

Young Generation

Most objects in the program live and die, so most newly created objects are stored in the new generation, unless large objects go directly into the old generation. The new generation uses replication algorithms to reclaim memory more efficiently.

The new generation can be subdivided into Eden, Form Survivor, and To Survivor. The default ratio is 8:1:1, which can be set by -xx :SurvivorRatio.

The implementation process of new generation garbage recycling:

1. Copy the surviving object From Eden + From Survivor To Survivor;

2. Clear Eden and From Survivor partitions;

3, From Survivor and To Survivor swap (From changed To, To changed From).

The Tenured Generation

The recycling frequency of old generation garbage is lower than that of new generation garbage, and the main objects of storage are:

1. The new generation object is promoted to the old age after N GC.

This can be set by setting -xx :MaxTenuringThreshold=5. The default value is 15.

2. Store large objects directly to the old generation.

Large objects are objects that require contiguous storage space, such as arrays.

When large objects cannot be stored in the new generation, an allocation guarantee mechanism is required to copy all objects in the current generation to the old generation. Since the allocation guarantee mechanism requires a large number of copies, which can cause performance problems, the best solution is to directly store large objects in the old generation.

Through the parameter – xx: PretrnureSizeThreshold to set the value of the object.

Note: This parameter is valid only for Serial and ParNew garbage collectors.

Serial

Serial’s oldest garbage collector, the only one in the new generation before JDK 1.3.1, uses a single-threaded Serial collection approach, which performs well in a single-CPU environment because there is no thread switch for single-threaded execution.

Thread type: single thread

Using algorithms: Copy algorithms

Specify collector: -xx :+UseSerialGC

Serial Old

Older versions of the Serial collector were also single-threaded. It has a practical use as an alternative to the CMS collector, which will be covered later when WE introduce CMS.

Thread type: single thread

Use the algorithm: mark-tidy

Specify collector: -xx :+UseSerialGC

ParNew

ParNew is a multi-threaded version of Serial and can share many control parameters with Serial, such as -xx :SurvivorRatio. ParNew can be used with CMS.

(Note: The picture comes from Zero One Technology Stack)

Thread type: Multi-threaded

Use the algorithm: copy

Specify collector: -xx :+UseParNewGC

Parallel Scavenge

Parallel is similar to the ParNew collector in that it is multithreaded, but Parallel is a throughput first collector, where the GC pause time is reduced at the expense of throughput, such as 100MB of memory, which takes 10S to collect. CMS will shorten to 7S to collect 50 MB of memory, so the pause time does shrink, but the collection frequency increases, and throughput decreases.

Thread type: Multi-threaded

Use the algorithm: copy

Specify collector: -xx :+UseParallelGC

Parallel Old

Parallel Old is an older version of Parallel, also a throughput first collector.

Thread type: Multi-threaded

Use the algorithm: mark-tidy

Specify collector: -xx :+UseParallelOldGC

CMS

CMS (Concurrent Mark Sweep) is a collector whose goal is to achieve minimum pause times and is well suited for B/S systems.

Use Serial Old to defragment memory.

CMS operation process:

(Note: The picture comes from Zero One Technology Stack)

1. Initial mark

Mark objects directly associated with GC Roots that need to Stop The World.

2. Concurrent markup

Analyze the reachability of the heap from GC Roots to find the live objects.

3. Relabeling

The re-mark phase corrects the mark record of the part of the object whose mark changes due to user actions during concurrency. The pause time in this phase is generally slightly longer than in The initial tagging phase, but much shorter than in The concurrent tagging phase, and also requires Stop The World.

4. Concurrent clearing

Get rid of garbage objects.

CMS faults:

1. Sensitive to CPU resource requirements.

The CMS collector is overly dependent on the multi-threaded environment. By default, the number of open threads is (number of cpus + 3) / 4. When the number of cpus is less than 4, the CMS has a significant impact on the user’s own operations because half of the computational power is allocated to executing the collector thread.

2. CMS cannot clear floating garbage.

Floating garbage refers to the garbage that the CMS removes and the user thread generates new garbage. This part of garbage that is not marked is called “floating garbage” and can only be removed in the next GC.

3. CMS garbage collection will produce a large amount of space debris.

CMS uses a mark-sweep algorithm, so a large amount of space debris will be generated during garbage collection.

Note: In the CMS collector, when the memory usage in the old generation exceeds a certain percentage, the system will do garbage collection; When the remaining memory cannot meet the program running requirements, a Concurrent Mode Failure occurs and the system uses the Serial Old algorithm to temporarily remove the Failure, which degrades the system performance.

Thread type: Multi-threaded

Use the algorithm: mark-clear

Specify collector: -xx :+UseConcMarkSweepGC

G1

G1 GC This is a throughput and pause time GC implementation that is the default GC option in JDK 9 onwards. G1 can intuitively set pause time goals. Compared with CMS GC, G1 may not be able to achieve delayed pauses in CMS at best, but it is much better at worst.

The G1 GC still has the concept of years, but its memory structure is not a simple strip partition, but rather a chessboard of regions. Region to Region is a copy algorithm, but the overall algorithm is actually mark-compact and can effectively avoid memory fragmentation, especially when the Java heap is very large, the advantage of G1 becomes more obvious.

G1 throughput and pauses are very good and still improving, while CMS has been marked deprecated in JDK 9, so the G1 GC is worth getting to grips with.

G1 Running process:

1. Initial mark

Mark objects directly associated with GC Roots that need to Stop The World.

2. Concurrent markup

Analyze the reachability of the heap from GC Roots to find the live objects.

3. Relabeling

The re-mark phase corrects the mark record of the part of the object whose mark changes due to user actions during concurrency. The pause time in this phase is generally slightly longer than in The initial tagging phase, but much shorter than in The concurrent tagging phase, and also requires Stop The World.

4. Screening and recycling

First, the collection value and cost of each Region are sorted, and a collection plan is made based on the expected GC pause time of the user. This phase can be executed concurrently with the user program, but because only part of the Region is reclaimed, the timing is user-controlled.

Thread type: Multi-threaded

Use algorithms: copy, mark-tidy

Specify collector: -xx :+UseG1GC (available after JDK 7U4)

reference

In-depth Understanding of the Java Virtual Machine

Algorithms and Implementation of Garbage Recycling

The last

Pay attention to the public account, send the keyword “GC”, receive “Garbage Recycling algorithm and Implementation” learning materials.