Jvm memory recall ultimate esoteric: garbage collection algorithm

[This article first appeared to java technical blog on code ]: jdkcb.com/

Preface:

An article on the exhaustion of nonsense we say to say how an object is determined to be dead, and mentioned java garbage collection bureau experienced years of development, invented a java experience dead instrument , which at one stroke solve the ordinary java object , and examination of the victims, a significant recovery difficult problem, has been unanimously praised java virtual machine all the leaders of the General Administration. Ahem, beside the point, of course, there is only this one artifact is not enough java garbage collection bureau won the annual Best Organization Award, jvm order to more easily carry out garbage collection, also invented a java garbage up to 7 recovery device, which are collectively referred to as:

Garbage recycling target instrument! (A Dream Sound)

In the " garbage, master the core technology faith", and garbage recycling target device is also driven by the four principles of algorithm, they are:

Mark - sweep algorithm, replication algorithm, sorting algorithm marks, generational collection algorithm.

basic concept:

java carve-generation garbage collection algorithm java heap by the survival time is divided into two regions, a new generation used to store objects, one of the old generation used to store objects.

New Generation: the new generation of easy to understand more, I specifically made up a nursery rhyme (funny):

Cenozoic, Cenozoic

Small objects, die fast

You poisonous it.

Older Generation: especially those who live a long time object.

Mark - sweep algorithm (Mark-Sweep):

Mark - sweep algorithms, all garbage collection algorithms is the most basic one, gather in front of the algorithm is a brother in the other garbage. The waves pushed before the waves it, clears the algorithm re-mark the beach, and most of us as the law of development, a lot of behind the algorithms are based on mark - sweep algorithm improvements comes.

Mark - sweep algorithm is a first mark and then cleared algorithm (which you do not Tuoliaokuzi put ..), the specific recycling process is like this:

  • Marking all objects need to be recovered
  • After completion mark these objects are marked uniform recycling

He did not, or else it easy to use them.

But at the same time achieve a simple also brought a lot of additional problems, which mainly focused on two points:

  • First, the first is efficiency itself clear labeling of these two processes is not too high, because you have to find an object, to give them cover chapter, and then one by one to clear, certainly not the side of the pot quickly.
  • The second problem is the use of space, because the in-memory objects that need to be recycled is not so lined up next to each other waiting for you to recover, but scattered throughout the memory, clear when a Java virtual machine is to be child to a children's run, and then go to recovery, because objects are scattered throughout, so we finished the recovery is likely to see this small memory is empty, the bit of the memory is empty, so will go on leading to memory are small chunks of memory, when a relatively large object needs to allocate memory, the system can not find such a large block of contiguous memory, can not find the system very anxious ah, so he had to advance In addition to trigger a garbage collection routines.

Figure this is probably a long way, drawing from Denver (Liu Wang Shu) Gangster:

img

Such binding text look is not easy to understand more.

Replication algorithm (Copying):

Mark - After clearing out the algorithm, Virgo could not stand.

Virgo: You look at your recovery that is what stuff, complete recovery of memory that mess, oh the mother I did not see the law, it can not tidy, neat how good a person.

So replication algorithm came into being. (Purely nonsense, do not take it seriously)

This algorithm does, in accordance with the memory capacity is divided into two equal-sized two, every time it, I only use one, the other one to keep, when the memory of which one runs out, put still alive copy objects to another piece up, and then used this once cleared her direct, simple and crude, efficient operation , because it is a whole chunk of memory recovery time, so what is the problem of memory fragmentation does not exist, but there is no free lunch, do also to pay a certain price, that is, I had 100M of memory, and now I use only once 50M.

It's like you have a whole pack of hot strip, but only once let you eat half a bag, think about the bad.

Replication algorithm it is generally the main target for the new generation of recycling, and why? Because the new generation, die fast .

Because the new generation of objects will be recovered quickly away, so in general we do not follow a less than a minute, the hotspot virtual machine, the memory is divided into three, one is relatively large Eden (Garden of Eden), and two relatively small survivor (survivors) space, each with an Eden and a survivor, when you need to recover, these two memory alive object is copied to another survivor in a one-time, just clean out and then directly used eden and survivor space. In the hotspot virtual machine, the proportion of Eden and Survivor defaults to 8: 1, later by modifying the virtual machine parameters to modify the ratio between Eden and Survivor, after my meticulous brainstorming-like computing (someone counted Okay) , with this proportion replication algorithm in the new generation of memory waste rate of only 10% in the hotspot virtual machine, indeed much lower than 50 percent above.

Wait, you do not say survivor smaller space, two memory live objects are copied onto a single survivor, survivor fit Zenong case. Stumped you now, I see how you do.

I did not, I can not take it , jvm introduced the concept of allocation guarantees when the survivor is not enough space, these objects will go directly to the old year by allocating guarantee mechanism. As for the distribution guarantee how specific is playing, you can access relevant information look Ha, I Typing hands hurt (in a low voice to force to force, you have no clear grasp Well, boo).

Since that replication algorithm performance so sweet, so why can not it be used to recycle old's region?

You think, what's old, java virtual machine longevity village, usually generally do not die, you put forward a copy algorithms have a look

Nima, are still alive

Then a lot of living a copy of old objects in the past, the situation is under extreme conditions, let alone a no dead a.

Yes, copy algorithm is as follows:

img

Mark - Collation Algorithm (Mark-Compact):

Do's nothing against the old way, the title has been written, it is surely there, that is Mark - sorting algorithm, I took it as "brothers ** are to the edge of Kaokao" ** algorithms.

Mark - sorting algorithm is especially suited years old, why, because not called markers - copy ah, mark - sorting algorithms and in fact mark - sweep algorithm principle similar, but not directly on memory consolidation, but let the brothers are to the edge Kaokao that let's old surviving objects are moved to the side, etc. after these are next to his brother moved together, directly to the rest of the memory directly cleared away.

img

Generational collection algorithm:

Generational algorithm nothing new things in front of that section in fact, that the basic concept of this algorithm, generational collection algorithm according to the different periods of survival target of the objects into the new generation and the old's two kinds of generational collection algorithm benefits is to achieve the right medicine, so that we can use different garbage collection algorithms based on different objects, such as the new generation, die fast (again), with a copy to the algorithm, I just use the old generation mark - sorting algorithm, or mark - Clear algorithm to recover.

Gone

This really did not chart

to sum up:

In this article we are more careful to say that our garbage collection algorithms Four Little Dragons, also cited examples such as spicy strips to help people deepen their understanding. Four kinds of garbage collection algorithms have advantages and disadvantages, and thus derived 7 commonly used garbage collector, garbage collector on the article, let's update late again.

You can be multi-point update?

Do not care more, you follow me ah.

I am concerned about you

it is good

I is the number of Han, we next article bye.

Guess you like

Origin juejin.im/post/5d5ca0fbf265da03e921cdde