Entangled with me, I finally gave my JVM series a third name, harm, I was really too difficult. From JVM to five minutes a day, to playing with JVM and now to talking about JVM to product managers, although content is king, the title can let more people see my article. Therefore, AFTER three topics, I finally decided on this one.

Let me tell you a little bit about the origin of this name. From the perspective of knowledge acquisition, the most in-depth way is to share knowledge with others. So why do I want to share knowledge with the natural enemy of programmers, the product manager?

The answer to that question is simple, because my “wife” is a “product manager”.

When I met her, she was Java development, who knew that the certificate became a product. Do you have a lot of friends?? (Hand stand


Nonsense not to say, the following began to enter our body ~

background

On a bright afternoon, I had just finished reading section 3 of Chapter 3 of Understanding the Java Virtual Machine — Garbage Collection Algorithms — when my product man began his regular grilling

Product greatly: what do you learn every day, I look like very familiar??

Me:?? Did you forget that you were once a Java programmer, even the JVM?


Product greatly: JVM ah, basically forgot almost, or you tell me about, save our “development every day fool me”

So I became a programmer traitor

I: ok, then I’ll tell you about my harvest in this section.

Connected to set

I: before we begin, let’s review the last time, last time we talked about how to determine whether an object garbage objects (dead), generally there are two kinds of algorithm – “reference counting method and accessibility analysis method”, the virtual machine is currently on the market is the second most used – accessibility analysis.

Product: Do these two methods correspond to two types of garbage collection algorithms?

There are two types of garbage collection: Reference Counting garbage collection (WGC) and Tracing garbage collection (WGC). These two types of garbage collection algorithms belong to the category of Tracing garbage collection. But before I introduce garbage collection algorithms, I need to introduce you to a key theory — generational collection theory.

Product big: ok

Generation collection theory

Me: In a sense, the so-called generation collection theory is a kind of convention commonly known as the summary of norms and experience, rather than a very strict rule.

Product big: HMMM, convention is bigger than configuration, the idea is reflected in many places now, so what is generational collection?

Me: Listen to me. Generational collection is based on three hypotheses:

  1. The vast majority of our subjects die young.
  2. The more GC an object goes through, the harder it is to die.
  3. Intergenerational drinking accounts for only a small proportion of intergenerational references.

Product big: I seem to have seen these three before, but I can’t remember clearly, can you tell me more about them

I: Because most objects are facing life in death, and survived more GC object many times more difficult to eliminate, so naturally will divide the object into two factions, one is easily happened GC, one kind is extremely difficult to GC, easily happened GC’s life cycle is shorter, so also known as “new generation”, extremely difficult to GC object with a long cycle life, So you could call them the old days.

Product big: so say, I seem to understand a bit, but why want to divide generation?

Me: If we divide them into generations, we can divide the area, part of which is used to store the new generation, the objects of the new generation. We only need to pay attention to the objects that are not recycled, instead of marking the vast majority of objects that need to be recycled; Some are used to store older ages, where GC occurs less frequently. The frequency of GC for these two regions is different, so GC separately can save a lot of time and space for storing these objects.

Product big: I see, so why is there a third hypothesis?

Me: if there is a new generation of old s object reference, or old s object exists in the new generation of references, this phenomenon is what we call reference across generations, and in order to ensure the accuracy of accessibility analysis, we also need to traverse the old s objects, this will cause great performance pressure and burden. That’s where the third conclusion comes in. Why? If you think about it, a new generation object will be promoted to the old generation if, after several GC runs, it is still not recycled because of cross-generation references. So that solves the intergenerational problem.

Product big: so what are the measures corresponding to the third hypothesis?

I: the text is too too pale, this is suitable to use the graph to express, look good


I: such words, can very clear (proud face

Product greatly: understood, that we continue, whether want to enter the main topic

Me: HMM, I’m going to introduce you to the most basic algorithm, the tag clearing algorithm

Mark-clear

Product big: this I know, just like its name, mark, then clear, see name know meaning ha ha ha ha.

I: fierce, not the kui is the product of technical background, admire admire, really like what you say in that way, very simple two steps, mark — clear.


Me: but this way are too humble, although when objects are less, the efficiency is faster, but when the object is a multiplied, tags and removal efficiency will be decreased, and this way will be a problem – memory fragments, if left blank, can’t put a larger object, will trigger another GC in advance.

Product size: There is something missing here, is there a different GC for different generations?

Me: Yes, I missed it, the GC for the new generation is called (Young GC), the GC for the Old generation is called (Old GC), and if the heap and method area are all collected, it is called Full GC.

Product greatly: so pour also calculate good to remember, OK, let’s enter the next algorithm.

Me: The next algorithm, which can be seen as an improved version of the first algorithm, is called the “mark-copy algorithm”.

Mark-copy

Me: Mark copy algorithm, the initial form is half area copy, that is, the previous content is divided into two equal parts according to the capacity, after one part is used up, the remaining surviving objects are thrown to the other half, and then the half is cleared, into another reserved piece. This is convenient and efficient if most objects need to be recycled (such as the new generation), but it is a way of sacrificing space for time. We only use half of the space efficiently, and half of the space is wasted.


Product big: So it’s bound to improve later, right? (look forward to face

Me: That’s right, the new replication algorithm can’t be called half-region replication, because the new generation memory range is split into three parts, one Eden region and two Survivor regions, and the ratio between them is 8:1:1. At the time of the GC, the use of a Eden and a Survivor area, and then put the rest of the live objects in another Survior area, clean up the rest of the region, the process of collecting, can complete a so wasted, only ten percent of the space relative to advantage, such shortcomings, we are perfectly acceptable,

Product big: What if there are not enough survivors left to drop the remaining surviving objects?

I: Ask good, this time coming to a new concept – “memory guarantee”, if not, will go to old age, if the old s “continuous space” total size is greater than the Cenozoic object or the average of the previous promotion, can lend Survivor area to use, as to why this is the case, and let me fetch a imprison son (actually I don’t know ha ha ha ha ha

Product greatly: so does this algorithm sound as if there are no shortcomings of the sub-sub-child?

Me: Not really, the operation of copying will reduce the efficiency of recycling, and if the recycling was carried out in the old era, who would you borrow it from? So there is a third algorithm – tag collation algorithm.

Mark-tidy

Product big: So what is mark finishing?

Me: The mark-defragment algorithm is a mobile algorithm that does not generate memory fragmentation by moving the living object to one side and then clearing the memory directly outside the boundary.


Product big: so the advantage of this algorithm is that it does not generate memory fragmentation, reduce the number of recycling, so it is better than mark-collation?

I: For, although at the time of moving objects, it will cost a lot of time and resources, but relative to mark – clear algorithm, the many hours spent in GC will more frequently, of course in the actual use process, the application of these three algorithms in different scenarios to use, such as the first to use the tag – clear algorithm, when memory fragments after reaching a certain degree, Different combinations have different effects, which I’ll explain when I introduce you to some common garbage collectors (next section

Product big: good ~ that today arrive here first, speak of almost enough I absorb of three or five days, lead two days you to tell me again ~

Me: Ok.

The last

Cough, if knowledge can’t feed you, then you can eat more dog food

Post a note of the product to me.


Is “good” just 🍋 link below, if there is anything want to give me, with my WeChat ID in above, can add my WeChat, we come to the further communication, of course, if you think of my article is interesting, can learn many things, please give me a “focus on + watching”, let’s go with product much learning. If you can follow a wave of public account “Vi’s technical blog”, THEN I feel grateful, for the “original author”, attention and reading is the biggest support and help to me!


Finally, sneak in a cute photo of my product peeking at my study.


Finally remind everyone a bit, “to product manager can speak truth can skin”, can not give the wife, don’t ask me why know, I go to “kneel keyboard”, not over to be continued, we next period goodbye (wave