GC Recovery Algorithm--when your girlfriend broke up with you!

Source: Internet
Author: User

The Java language introduced the garbage collection mechanism, so that the C + + language of the headache of memory management problems solved, so that our Java dog happily every day to create objects without the control of the object, these are the Java garbage collection mechanism brings benefits. But what is the core principle of Java's garbage collection mechanism? Let's talk about the GC recovery algorithm today.

The GC recovery scenario of the JVM is complex, not a single algorithm can be done, can be divided into the accessibility analysis algorithm, marker-clearing algorithm, marker-collation algorithm, generational recovery algorithm, replication algorithm .

In the square, the girlfriend suddenly broke up with you, and then head not back to a person to go, leaving you a person standing under the tree, BGM slowly sounded "snow north wind squealing the world a vast expanse ~ ~ ~" leaves have fallen, at this time you seem to be in Charlotte Yuanhua possessed, became the world's most sad person. When you are immersed in sorrow can not extricate oneself, next to the sanitation aunt a face to look at you "young man you move, do not block me to sweep the floor."

Yes, you're right. The fallen leaves on the ground are the core of the GC garbage collection algorithm-the accessibility analysis algorithm .

Accessibility analysis algorithm

The breeze at first, yellowing of the leaves have fallen, just broke up you can not help but sigh, " leaves are the pursuit of the wind or the tree does not retain ", when the leaves from the branches falling from the moment, it is no longer related to the tree. Similarly, the basic idea of the accessibility analysis algorithm is that the objects in the JVM memory are managed in the form of trees, which we call "GC tree". The root node of the GC tree is called GC Roots, and through some column GC Roots as the starting point, the search goes down from these nodes, and the path that is traversed is called the reference chain, proving that the object is not available when an object is connected to a GC Roots without any reference chain.

Objects 1, 2, 3, 4 in the figure can find a reference chain connected to the GC roots, belong to the surviving object, and the objects 5, 6, 7, although they are related to each other, but they to the GC roots is unreachable, so belong to the dead object.

What are the objects that can be used as GC roots?

Referenced objects in the virtual machine stack (local variable table in the stack frame)
Object referenced by class static property in the method area
The object referenced by a constant in the method area
Object referenced by Jni (native method) in the local method stack

Tag-purge algorithm (MARK-SWEEP) breakup is not a two-time mark.

When a girlfriend breaks up with you, does she really want to tell you? Too naïve boy!!! Women are emotional animals, knife mouth tofu heart, she just gave you a reprieve, if you do not do anything, then I have no words, bet solitary boy, if you are correct, then you have to be saved!

similarly , when a GC thread traverses a GC tree to detect a useless object, it is not immediately a humanitarian destruction, but it is marked first, telling the object that you have been shot on the list. Here is the first time Mark .

What should I do to retain love? Say sweet words coax her, pull her to hoping at restaurant to eat good, or go to the mall to buy her long-cherished Dior 999 lipstick ... These routines I will not say, anyway as long as can make a girlfriend happy, what pay is worth.

When the object is first marked, theGC thread checks to see if it is necessary to execute the Finalize () method . Finalize () method do you remember? Finalize () Definition:Finalize () is the protected method of object, which can be overridden by subclasses to implement resource cleanup, which is called by the GC before it reclaims the object. What do you mean? This means that if we rewrite the method, the method will be executed before the GC is reclaimed.

However, there are two scenarios in which GC threads do not consider it necessary to execute.

1. The object does not overwrite finalize (). Girlfriend to break up with you, you are like nothing else to go home to continue to play the game, young man your heart is really big.

2.finalize () has been executed by the virtual machine. Girlfriend at this time heart OS: Last quarrel you sent me a Dior 999 amends, this time you send again, you do not know I have been wanting to buy turnip ding in this period of time? Must be in the perfunctory me! Oh! Man!

to live or die to love or to be free that's a question!

If the object is judged to need to execute the Finalize () method, then it will be placed in a queue called F-queue to wait for execution, the object of the opportunity to redeem themselves! If an object wants to successfully save itself in Finalize (), simply re-associate with GC roots, such as assigning itself to a class object or a member variable of an object, it will be removed from the collection that is about to be recycled at the second token . If it has not been associated at this time, then it is really GG, we use a "cool" to give it a farewell bar.

Graphic:

The above is the mark-and-sweep algorithm, but it has two points of inadequacy :

1. Efficiency issues, marking and cleaning processes are not efficient.

2. Spatial problems, when the mark is cleared, generates a large amount of discontinuous memory fragmentation, and too many fragments can result in the subsequent allocation of objects in the program that occupy a larger contiguous space (such as arrays), where sufficient contiguous memory cannot be found and the next garbage collection action has to be triggered in advance.

In order to solve these problems, "replication algorithm" came into being.

Replication algorithm (Copying) species Great Escape Noah's Ark!

The idea of copying algorithms is simple: divide the memory into two blocks of equal size, using only one piece at a time. When a piece of memory is full, copy the object that is still alive to another piece, and then completely empty the previous memory space. It's a bit like a story in the Bible: The floods are coming, and the creatures flee to the ark to avoid disaster. In this way, every time the entire half of the memory is collected, memory allocation time also do not consider the situation of memory fragmentation, simple rough let people like! But this algorithm will reduce the memory for the original half, the price is too high, we have to know, memory is a valuable resource!

Golden Ratio 8:1:1

Medical research has shown that colds are caused by viruses. Cough up a joke! Software team research shows that most of the objects in memory are "ephemeral", so there is absolutely no need to play according to the ratio of 1:1. Instead, it divides the memory into a larger Eden Area (Eden area) and two smaller survivor areas (surviving areas), each using the Eden area and one of the surviving areas (we take the individual named survivor number 1th and the other named Survivor 2nd). When the recovery, the Eden area and the survivors of the 1th area of the object in one-time copy to the survivor 2nd inside, and finally to the Eden District and survivor 1th to liquidate, all the objects in the survival or death completely clean up, than the bully hit a finger even worse!

Graphic:

Now that the hotspot virtual machine default Eden area and two blocks of survival area of the size of 8:1:1, so usually when working, only 10% of the memory will be wasted, this is not a good deal?

See here is a shoe will ask, why the survival zone divided into two pieces? The ratio of 9:1 is perfect, right? No!no!no!, here we have to draw up another algorithm--the generation of collection algorithm .

Generational collection algorithm (generational Collection) tested to fruition

Have found that the so-called love is actually going through countless times of the run-in, countless times of the test, together with the two people only withstood these hardships, will enter the palace of marriage, with a happy ending, and can not withstand these tests, the two sides only their own well, forget the lake.

Every time we recycle GC, a small number of objects will survive and live until the next GC is detected again. Recently very hot chicken game, players no matter what means, just gun or Gou, only to live to become the last survivor. The Java object is also the case, experienced GC layer test, and eventually become a small strong to kill. At this time, the GC is not happy, you are wasting my days, Jack Bauer heart OS "like to see you upset I do not lose my appearance!" "For this group of stubborn molecules, the GC as a law enforcement decision to see the net, so commissioned the JVM specifically divided into a region to their retirement, from the end of the horizon is passers-by." The division of this area is the famous " old age ", and the corresponding is the previous GC frequent "Cenozoic".

How can an object run from the "New generation" to the "old age"?

We create an object, its object header will have a GC generational identity, each time GC if you can survive the identity +1, when added to a certain number of times, the GC will determine that the object is an old rogue, so it moved from the "New generation" to the "old age", arrangements! Refer to my other blog "If Java objects are personal ... 》

Each time the new generation of GC, survivor 2nd survived the target Age ID automatically +1, and then determine whether the 15-year-old (the default 15 times), if the 15-year-old, then from the survivor 2nd copy to the "old age" to take a life, if not, then copied to the Survivor 1th area, Then the survivor number 2nd area was emptied.

Because of the extremely high survival rate of the old age, it is unnecessary to replicate the algorithm. Then someone proposed another algorithm called "marker-Collation algorithm".

Mark-and-Organize algorithm (mark-compact)

The mark-and-organize algorithm actually has the same basic process as the "tag-delete" algorithm, except that the next step is not to clean up the useless objects, but to let all surviving objects move toward one end and then clean the memory outside the end boundary directly. This perfectly solves the problem of memory fragmentation of the "tag-purge algorithm".

Graphic:

In this case, the Java GC Recovery algorithm is basically the same. Our GC is for different areas in memory, to take a reasonable algorithm to achieve the effect of automatic cleanup. The new generation of objects most ephemeral, the use of "Replication algorithm", the old age of the object survival rate is very high, the use of "tag-delete algorithm" or "marker-collation algorithm."

Is it not that the GC recovery algorithm is not as mysterious as it might seem? I hope my understanding can bring you a little help, because people lazy, pictures are online directly to use. In addition, if the reality of friction with the girlfriend, the Furuan still have to Furuan, the man should show a generous point, after all, two people get along hard, not to mention she will spend the rest of your life with you, do not because of a momentary impulse and regret life. Amount ... I seem to smell the sour smell of love again!

Reference: "In-depth understanding of Java Virtual Machine--JVM advanced features and Best Practices (2nd Edition)"

GC Recovery Algorithm--when your girlfriend broke up with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.