Diagram of GC in Java (generational collector)

Source: Internet
Author: User

In the Java garbage collection algorithm in front of the garbage collection algorithm in the generational collector, today saw a video found inside will also very good, so decided to summarize.

We know that in the generational collection algorithm The heap space is divided into the new generation and the old age. Because the survival rate of the new generation of objects is relatively low, so the general use of replication algorithm, the survival rate of the old age is generally relatively high, generally using the "mark-clean" or "marker-collation" algorithm for recycling.


The above diagram has clearly shown the partition of the heap.


Let's take a look at the specific algorithm process.

The newly created objects are generally placed in the Enden area of the Cenozoic, as shown in.


In the above object, green stands for "Survivor", and the gray represents "object to be recycled". When the Enden is used, a new generation GC, the minor GC, will occur.



First, the surviving objects are copied to the Survivor1.



Then empty the Enden.



Set object age to 1 after moving to Survivor1 space


This is the first time the GC is complete.


when the Enden area is used again, the GC operation is performed again. .


Above Enden and Survivor1, green represents the surviving object, and recycling represents "objects to be recycled," because in the process of using allocations for heap memory, there are also constant references to objects that cannot be reached.


During the GC again, the surviving objects in the Enden and Survivor1 are copied to Survivor2, as above. Note that the current is still in the new generation of GC, because the Cenozoic is divided into Enden, Survivor1, Survivor2 Three regions, the use is actually a replication algorithm.



Then empty the Enden and the Survivor1.



The age of the objects copied to Survivor2 in Enden is then set to 1, and the age of the objects copied to Survivor2 in Survivor1 plus 1



So the second generation of GC is completed. When Enden is used again, a third GC operation occurs.



The following basic repetition of the above ideas, the first Enden and Survivor2 in the survival of the object copied into the Survivor1.



Then empty the Enden and the Survivor2.



The age of the objects copied to Survivor1 in Enden is then set to 1, and the age of the objects copied to Survivor1 in Survivor2 plus 1



The back of the operation is basically repetitive, when will it enter the old age? As seen from the above, if the object is not recycled in the GC, then its object age will continue to increase, the object in the Survivor area each through a minor GC, age increases 1 years old, when its age reaches a certain level (by default, 15 years old), will be moved to the old age, This age threshold can be -XX:MaxTenuringThreshold set by setting.



Video link: Garbage collection in Java, with Animation and discussion of G1 GC


Diagram of GC in Java (generational collector)

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.