Garbage collector for beginners to learn java performance

Time:2021-6-11

preface

Garbage collection algorithm is the methodology of memory recovery; Garbage collector is the concrete implementation of memory recovery. Java virtual machine specification has no rules on how to implement the garbage collector, so different vendors and different versions of virtual machines provide different garbage collectors, and generally provide parameters for users to combine the collectors used in different years according to their own application characteristics and requirements.

The virtual machine does not use one collector, but many collectors are used together, and different collectors are used in different times.

Garbage collector for beginners to learn java performance

1、 Serial collector

Serial collector is the most basic collector with the longest history. It used to be the only choice for the new generation of virtual machine collection. This collector is a single thread collector, but its meaning of “single thread” does not mean that it will only use one CPU or one collection thread to complete the garbage collection work. More importantly, when it carries out garbage collection, it must suspend all other work lines until the end of collection“ In fact, the work of “stop the world” is initiated and completed automatically by the virtual machine in the background. It is difficult to stop all the normal working threads of the user when the user is invisible.

Garbage collector for beginners to learn java performance

2、 Parnew collector

Parnew collector is a multithreaded version of serial collector. Apart from using multiple threads for garbage collection, other behaviors include all control parameters (such as: – XX: survivorratio, – XX: preteneuresizethreshold, – XX: handlepromotionfailure, etc.), collection algorithm, stop the world, object allocation rules, and so on The recycling strategy is completely consistent with the serail collector, and they share a lot of code in the implementation.

Garbage collector for beginners to learn java performance

Parnew collector has not much innovation compared with serial collector except for multithreading collection, but it is the preferred new generation collector in many virtual machines running in server mode. One of the important reasons is that it can work with CMS collector except serial collector.

3、 Parallel scanvenge collector

Parallel scanvenge collector is a new generation collector. It is also a collector using replication algorithm and a parallel multithreaded collector.

The feature of parallel scavenge collector is that its focus is different from other collectors. The focus of CMS and other collectors is to shorten the pause time of the user’s current process when the garbage collector collects. The goal of parallel scavenge collector is to achieve a controllable throughput. The so-called throughput is the ratio of the CPU time used to run user code to the total CPU consumption time. That is, throughput = run user code time / (run user code time + garbage collection time).

The shorter the pause time is, the more suitable it is for programs that need to interact with users. Good response speed can improve the user experience, while high throughput can make efficient use of CPU time to complete the calculation tasks of programs as soon as possible. It is mainly suitable for tasks that need not too much interaction in the background.

Parallel scavenge collector provides two parameters to precisely control the throughput, namely – XX: maxgcpausemillis parameter to control the maximum pause time of garbage collector and – XX: gctimeratio parameter to directly set the throughput.

The maxgcpausemillis parameter allows a number of milliseconds greater than 0. The collector will try its best to ensure that the memory recovery time does not exceed the set value.

The value of gctimeratio parameter should be an integer greater than 0 and less than 100, that is, the ratio of garbage collection time to total time.

Note:

Parallel: it means that multiple incoming and collecting threads work in parallel, but the user thread is still waiting

Concurrent: the value user thread and the garbage collection thread execute at the same time (but not necessarily in parallel, may be executed alternately), the user program continues to run, and the garbage collection program runs on another CPU.

4. Serial old collector

The serial old collector is an old version of the serial collector. It is also a single threaded collector, which uses the “mark tidy” algorithm. The main significance of this collector is to use it with the virtual machine in client mode. If it is in server mode, it has two major uses. One is to use it with parallel sweep collector in JDK1.5 and earlier versions, and the other is to use it as a backup plan for CMS collector when concurrent mode failure occurs.

Garbage collector for beginners to learn java performance

5、 Parallel old collector

Parallel old collector is an old version of parallel scavenge collector, which uses multithreading and “mark tidy” algorithm.

Garbage collector for beginners to learn java performance

6. CMS collector

CMS (concurrent mark sweep) collector is a kind of collector which aims to get the shortest collection pause time. CMS collector is based on “mark clear” algorithm. Its operation process is divided into four steps

1) CMS initial mark

2) CMS concurrent mark

3) CMS mark

4) CMS concurrent sweep

The two steps of initial marking and re marking still need to “stop the world”. The initial tag just marks the objects that GC roots can directly associate with, which is very fast. The concurrent marking stage is the process of GC roots tracing, while the re marking stage is to correct the mark records of the part of objects whose marks are changed due to the continuous operation of the user program during the concurrent marking period. The pause time in this stage is generally slightly longer than that in the initial marking stage, but much shorter than that in the concurrent marking stage.

Garbage collector for beginners to learn java performance

7、 G1 (garbage first) collector

G1 (garbage first) collector is a garbage collector for server applications. Without calculating the operation of maintaining the recalled set, the operation of G1 collector can be roughly divided into the following steps:

1) Initial marking

2) Concurrent marking

3) Final marking

4) Live data counting and evaluation

Garbage collector for beginners to learn java performance

After three things ❤ ️

If you think this content is quite helpful to you, I’d like to invite you to do me three small favors:
Like, forward, have your “praise and comment”, is my creation power.
Pay attention to the official account of “Java didi” and share original knowledge without any time.
At the same time, we can look forward to the following articles