Jvm memory recall ultimate esoteric: garbage collector

Preface:

Before the death of a java object to prove that we talked about how an object is determined to be dead, and after the Jvm memory recovery ultimate esoteric: garbage collection algorithm was introduced more popular four garbage collection algorithm in detail later major virtual machine fully verify the feasibility of the four algorithms, there is a demand there will be the so-called markets, in the garbage, to master the core technology strong beliefs, garbage company came into being (not curse you sure?), the company introduced a breath of garbage covering seven major scene garbage collector for the majority of consumers, effortlessly occupied the market share of 90 percent, the company set up day, is rushed to market, while competitors were scheduled to go Africa, the story here, JVM universe officially formed.

Yes, the seriousness of nonsense.

The seven products were as follows:

  • Single-threaded garbage collection Little Prince: Serial Collector
  • Multi-threaded garbage collection little prince: ParNew collector
  • Throughput Priority Little Prince: Parallel Scavenge collector
  • Single-threaded garbage collection old prince: Serial Old collectors
  • Multi-threaded garbage collection old prince: ParNew Old collectors
  • The garbage collector rising star: the CMS collector
  • From the future of next-generation collector: G1 Collector

With excellent performance in the field of recycling of garbage, many times to save memory crisis, which was later known as garbage collection Alliance

Serial Collector:

First, the first play is the Serial collector, it is all collectors inside the oldest collector, before the era jdk1.3 barren substance, Serial collectors like IE is the only option, the garbage collection Serial somewhat similar to our usual way of cleaning aunt, when garbage collection, will tell all threads in the presence of:

You, you, you, do not move, just Saowan

Serial collector when working out other worker threads will be suspended until the end of its collection, which is unacceptable for many applications, such as you call a game java glory, is preparing to do battle group, Serial collector stand up and say, Kaokao first brothers to the edge, I want to collect the garbage, and give you a pause for five minutes, who can bear, here is the process of running Serial collector, which is used in the new generation of replication algorithm , using the old generation mark - collation algorithm

Readers see here would certainly say, this garbage collector is too rubbish, right, it is not true, Serial specificity (single-threaded) collected in only one core system and other recycled because the threads do not need to interact, but a little higher efficiency.

ParNew Collector:

ParNew collector does, in fact, is a multi-threaded version of the Serial collector, in which the collection algorithm used, Stop the word (pause type STW, suspend other threads), object allocation rule, recovery strategies are almost exactly the same with the Serial collector, but excel rather than blue, ParNew when the implementation of garbage collection is multi-threaded, of course, this is not sufficient reason for it to be based on the java virtual machine world, its greatest advantage is especially good relations and CMS, almost sworn brothers, the two co-operate full understanding, and these, Serial and Parallel Scavenge collector can only pales seen, a good teammate how important it is. ParNew working process as shown below:

You look at this chart a bit oblique ah, we do not care about these details

But parNew collector not to say that because the addition of multi-thread after blasting Serial collector, and when there is only one kernel, efficiency ParNew would not usually high Serial collectors, because a person is usually a small room to clean up than ten personal sweep up fast, because there is no cost to communicate with each other, but with the increase in the number of cores, the advantages of ParNew will gradually be reflected.

Parallel Scavenge collector:

Coincidence, Parallel Scavenge is a new generation of collectors, and ParNew collector in multi-threaded, replication algorithm and almost the same, he exactly where the powerful, yes, Idea, when other garbage collectors are desperate reduce user thread pause time when it another way, choose the throughput priority.

PS: Throughput = time to run code User / (+ time the user runs the code time garbage collector)

The higher throughput means that the user code running time is higher, incorporated herein by reference in-depth understanding of the original jvm virtual machine, the feeling of how I did not say it well.

The shorter the time the more suitable pause the program needs to interact with the user, good speed accordingly can improve the user experience, and high throughput can be efficient use of CPU time computing tasks to complete the program as soon as possible, mainly for operations in the background without too multi-task interaction.

There is a sense of a little low, the garbage collector does not map ,,,

Serial Old Collector:

Serial Old collector's edition Serial old collector, it is also a single-threaded collectors, using the mark - Collation Algorithm , which mainly work in Client mode, because the Parallel Scavenge collector and Serial cms collectors and relations are not well, all they two became good friends, a new generation, a decade old, it can be used in conjunction with the second CMS collector as a back-up plan. FIG its work as follows:

ParNew Old Collector:

The same, ParNew Old ParNew collector is a collector's version of the old, the use of multi-threading and marking - sorting algorithm , as we said above, Parallel Scavenge collector and collector CMS particularly undesirable, so if once you choose Parallel Scavenge collector as a new generation of collectors, then it means that the old collector's our only option Serial old collector, collectors and Serial old belong to a single-threaded processor, will inevitably have a performance problem, so this relatively boring, is simply bundling it all up to launch ParNew Old collectors had been addressed, so far, Parallel Scavenge collector + ParNew Old collector is the real priority is to achieve a throughput to attain combination.

The figure is a schematic view ParNew Old collector runs:

CMS collector:

CMS collector, similar to the full name of the Concurrent Mark Sweep collectors, and most collectors, is also committed to shorten the garbage collector thread pause time. Itself uses advanced version of the mark - sweep algorithm :

Divided into four processes:

  1. The initial mark
  2. Concurrent mark
  3. Relabel
  4. Concurrent Clear

Where concurrent and concurrent mark and clear both to do the real work occupies most of the time the entire garbage collection process. The initial tag can be said to come a little early to look at who needs to be marked, it does not take much time, then re-marking which is marked to see the changes, but also quickly completed.

The implementation process is as follows:

Although the CMS collector Star collector is today, but not without flaws, of which there are three obvious problems.

  • The first is the use of CMS collector mark - sweep algorithm can cause a lot of space debris, the reason I have explained in the article on garbage collection algorithm, friends do not understand can take a look.
  • The second is very sensitive to CPU resources: we would like, so flowchart multi-threaded, multi-time when the CPU can accept, I have healthy thick, you can almost eat more support from, but the relatively small number when the CPU time, the equivalent of a mother raising a bunch of starving children thread, it will be very difficult, will lead to a corresponding decline in our application performance.
  • The third is no way to recover "floating garbage": This floating garbage is what this means, so we understand, do you sweep the floor at home, melon seeds next to your brother, when you sweep your brother over there, you edge sweep it is to your side swept clean place still, however, provision virtual machines you can only scan once, can not go back, so you have to wait at a sweep when handling garbage. Therefore, CMS can not refuse what new thread in the process of recovery because of the run generated recovery, that is, floating garbage.

G1 Collector:

G1 collector I call it the next generation garbage collector , a garbage collector from the future, real speaking, G1 garbage collector is one of the forefront of the development of the results, however, due to some unresolved issues so the delay can not be listed until JDK7 formally join the family jvm virtual machine, it appears as the default garbage collector JDK9.

G1 signs are not engineers think the garbage collector just add a little longer, according to our previous historical experience, the birth of a new technology must be to compensate for the lack of the old technology, G1 also in order to make up for some of the CMS garbage collector the birth of the problem, with respect to the CMS garbage collector, G1 major upgrade, mainly in the following aspects:

  • G1 has an advantage in terms of compression space
  • By way of the G1 memory space divided into regions (Region) avoiding memory fragmentation Eden, Survivor, Old unsecure area, more flexible in terms of memory use efficiency is
  • G1 garbage collection time can be controlled to avoid application of the avalanche phenomenon by setting the expected pause time (Pause Time)
  • G1 immediately after the recovery of memory free memory consolidation while doing work, and the default CMS is doing STW (stop the world) when
  • G1 will be used in Young GC, whereas CMS can only be used in the O Zone

Compared to the other terms of the garbage collector, G1 mainly has the following four characteristics:

  • Parallel and Concurrent : better take advantage of multi-CPU.
  • Generational collection : this on the powerful, not strong enough to mate, can manage the entire GC heap, a person carry a full memory.
  • Spatial integration : Schrodinger's garbage collection algorithm, the overall look is based on the mark - sort of algorithm, but also like to see local-based replication algorithm, but no matter which of the two they use almost no memory space debris.
  • Predictable pause: a bit like AI, will establish a predictable pause time model can be planned to avoid the entire Java heap garbage collection to achieve region-wide.

The G1 is the implementation process is still relatively complex, where we will not pay more narrative, the students want to know can go to search for relevant information.

And the same CMS, G1 garbage collection process is also divided into four stages:

  1. The initial mark
  2. Concurrent mark
  3. The final mark
  4. Filter Recycling

Flowchart is as follows:

to sum up:

In this article we are more little early to say, said jvm seven kinds of garbage collectors, have their own strengths, there is no absolute beating and hanging, only selected scenes. Suitable scene with the appropriate garbage collector, will make our application performance is improved.

I is the number of Han, concern me, hello fruit to eat (hum)

Like walking a point oh.

Wait:

Related Items welcome to my github download (welcome star):

github.com/hanshuaikan…

Guess you like

Origin juejin.im/post/5d85d093f265da03e71b3080