preface

In the previous article, we had a general understanding of the garbage collection algorithm in the JVM. It was a rainy weekend at home and we started a new round of learning:

Last weekend, we talked about garbage collection algorithms. What about the applications of these algorithms?

I: of course, if garbage collection algorithm is beat dog stick method, so garbage collector is the previous beggars help Lord, different help Lord comprehend the nature is also different, I first of these help Lord carry on a simple introduction, see pictures!


Me: I from the recycling area to the garbage collector for a simple division, can be roughly divided into nine such, and listen to me for you.

Product big: Good

Serial collector

Me: Serial is a very old “new generation collector” that dates back to the JDK or the 1.x era (when I was about five?

Product big: As the name Serial suggests, this is a Serial collector, right?

I: The Serial collector is a single-threaded collector and its collection process is very simple (see figure below). Although it only uses one processor or one thread to collect garbage, I don’t like to call it a single-threaded collector. It is more appropriate to use the word “Serial” because when it does GC, Must suspend other threads (Stop The World, STW)


Product size: so is this collector obsolete, just in history, as something worth remembering?

I: in fact, can not say so, although it is simple, but simple has simple benefits, because of the single thread, so there is no switching thread resource overhead, so in the environment of single-core CPU, its performance will appear more excellent, can only say that there is no best collector, only the most suitable collector ~

Product greatly: maozi oh, so we talk about the next collector, this I have a general understanding ~

ParNew collector

Me: The next collector, called the ParNew collector, is basically the same as the Serial collector, except that it starts multiple GC threads for garbage collection (parallel collection)

Product big: Parallel? I’ve only heard of concurrency.

I: Parallel refers to the relationship between worker threads and garbage collection threads, while parallelism refers to the relationship between multiple garbage collection threads. These two concepts are different. When parallel, the worker thread is in the wait state. In concurrency, the worker thread is active.

The product is big: how does it compare to the Serial collector?

Me: Serial’s GC efficiency is better in a single-threaded environment, but it can be very effective in a multi-threaded environment for efficient use of system resources in garbage collection.

Product big: so it’s ok now ~

Me: : It was the first collector to be phased out of the HotSpot VIRTUAL machine, and in JDK 9 it was practically incorporated into the CMS collector and a little reveal. CMS was also removed in JDK 14, ending its reign on the server side.

Product big: good poor, woo woo woo

Me:?? Stop your show. Let’s move on


Parallel avenge

The Parallel Insane collector is the same as the ParNew collector. The ParNew Collector is insane.

Product big: this is to test me?

Me: Tell me

Products greatly: although both goods look similar, but I feel like WeChat and QQ difference, the two goods while it also is very similar, but the difference lies in the emphasis is different, different market segments will make them although the conflict, but there won’t be out as malignant competition relations, and prefer to complementary, combination of Yin and Yang.

I: say of really good, this mouth true ability cheat cheat, no wonder every day development write not over of demand (silently broken broken read aloud 100

Me: It focuses more on throughput, what does throughput mean, it doesn’t Care so much about the interval of each STW, but rather an overall utilization of the resource.

Product big: So how do we control throughput?

Me: There are two parameters: -xx: MaxGCPauseMillis and -xx: GCTimeRatio. -xx: +UseAdaptiveSizePolicy is a Boolean value. If this switch is enabled, the vm automatically adjusts the first two parameters to achieve an adaptive effect. This parameter is also a Parallel insane, distinct from ParNew.

Serial Old collector

I: The Serial Old collector is, as its name suggests, a single-threaded collector like its successor, Serial. Unlike the new generation Serial, it uses a tag collation algorithm.

Product greatly: this couple ID TSK TSK, I have imagined to see the two names of the program ape, cold dog food to mouth random plug

Me: : Well, they’re insane, but they can be used with the Parallel Insane, and it’s a backup to CMS.

Product greatly:?? Hetui, men don’t have a good thing, next, next

Me:?? It’s none of my business. When men sit at home, POTS come from heaven


Parallel Old collector

I: The Parellel Collector is a family of the Parellel Scavenge collector. The Collector is insane. Serial Old is the original version of Parallel Scanvenge. Before it came out, Serial Old was playing the role of Parallel Scanvenge, but after it came out, it kind of drove out the other three and became the original.

Product big: originally this is the original oh, that is to say if we value throughput, we can use their ‘husband and wife’ combination to complete (husband and wife, work not tired

Me: Yes, that’s right. Let’s take a look at one of the most important garbage collectors in the history of the JVM.

Product big: good good ~

CMS collector

I: CMS (Concurrent Mark Sweep) collector is the first collector to take the word concurrency into account and gain widespread application and promotion, that is, it can simultaneously dump garbage and collect garbage.

Product big: so what is its running process, must be different from other ~

Me: Look at the picture


Me: The first thing we know from its name is that it’s based on the mark-sweep algorithm and it’s divided into four steps:

  1. Initial marking (this process is very short and simply marks objects that GC Roots can be directly associated with
  2. Concurrent flags (run with the user thread, which takes longer, but does not cause STW
  3. Relabeling (corrects changes during concurrent tagging that take longer than the initial, but much less than the concurrent tagging time
  4. Concurrent cleanup (Run with the user thread for concurrent cleanup

Product big: So CMS is so good, are we using it all the time?

Me: No, the general epoch-making end will not be very good, because it has three problems:

  1. It consumes a lot of processor resources and is sensitive
  2. In the process of GC, it is easy to generate new garbage, if the generation of floating garbage leads to concurrent failure, then Serial Old is enabled, which is very slow
  3. Since it is implemented based on the mark-clean algorithm, some fragmentation space is created, and when the fragmentation space is insufficient to hold objects, the Full GC is triggered

Product big: so this is what it looks like now

It was marked “Deprecate” in JDK 9 and has been deprecated in JDK 14

image-20200326223034422

Me: Let me introduce a current collector we use, also known as G1 collector ~

G1 collector

Me: Garbage First, also known as G1, is an emotionless killer with its cool appearance, ruthless performance, and futuristic design

Products greatly: speak human language (manual smile face


Me: The G1 collector is another milestone that pioneered the local collection-oriented design and region-based memory layout of collectors.

Product greatly: this words how solution?

I: In the previous collector, we divided the memory area into old and new generations based on the theory of memory generation, and different collectors were used to complete the different regions. G1 subverts this concept. It no longer distinguishes between the so-called old and new generations, but collects for the whole heap. The Java heap is divided into independent regions of equal size. When garbage Collection is carried out, the regions that need to be collected are combined into a Collection Set. The region that holds the most garbage and has the greatest Collection benefit is collected first. That’s how Garbage First got its name

Product size: Then I have a question, what if the object size is too large for a region to fit into?

I: First of all, although the whole is divided into parts, the concepts of old age and new age are still preserved. However, this area is not a fixed Region, but a dynamic collection of a series of regions, and these large objects will be stored in N continuous Humongous regions. These regions are considered part of the old age

Product big: so compared with CMS, what are its advantages?

I: Compared with CMS, the most fundamental point is that CMS is based on the “mark-clean” algorithm, while G1 is based on the “mark-tidy” algorithm, and the “mark-copy” algorithm between each region. This approach does not produce a large amount of memory fragmentation, which can make the program run stably for a longer time. Save the next Full GC because a large object cannot be allocated.

Big product: The drawback of THE G1 is obvious. It is very powerful, and brings a load on memory and pressure on CPU. After all, there is no such thing as a free lunch

I: yes, because the CARDS used in dealing with cross reference Region table technology, tags – the replication algorithm will result in its memory pressure to 20% higher than a CMS, but whether it’s for it brings ascension and bonuses, or for the generation of new appointee in historical development, we are more inclined to choose the G1 ~

Product big: HMM, below still have ~

Me: Here’s a brief introduction to the two experimental collectors. I don’t know what will happen to them in the future, but they are as predictable as the G1

Product big: good ~


Shenandoah collector && ZGC collector

Me: Here is a brief introduction to these two low-latency collectors. In our collectors, there are three main criteria for evaluation: memory footprint. Throughput. Delay, among the three, the best we can do between two extremes, with the development of our computer hardware, less important memory footprint slowly, people are becoming more and more emphasis on delay, so low latency is called the goal we pursue, and both the collector is in order to achieve the objectives of the GC just 10 ms to ~

Products big: So what exactly are they?

Me: One is called Shenandoah and one is called ZGC. They are very similar in function, except that ZGC is the biological one and Shenandoah is the adopted one. Both of them can be regarded as the successor of G1, and G1 also borrowed and improved on these two codes to some extent.

Me: The ZGC collector is a garbage collector based on Region memory layout, with (for now) no generation, using read barriers, dye Pointers, and memory multiple mapping techniques to achieve concurrent mark-collation algorithms, with low latency as the primary goal. Its technical implementation, I will not go into detail here, when it is more widely used, it is not too late to study


Product greatly: ok ~ that we arrive here today, next time we continue, we now go out to masturbate string ~