G1 garbage collector in the JVM

Source: Internet
Author: User

Let's review the main Java garbage collector (HotSpot JVM). This article is discussed for garbage collection of heaps.

The heap is decomposed into a smaller three-part. Specifically divided into: The new generation, the old age, lasting generations.

    1. Most of the newly generated objects are placed in the Eden area, and when the Eden area is full, the JVM will trigger the young GC for not having to request memory, and the Survivor area (set as S0 area) of the Eden Area + object is garbage collected, and the surviving objects are copied to an empty survivor using the copy algorithm. (S1), the Eden area is emptied, and the other survivor S0 is empty. The next time the young GC recycles EDEN+S0, the surviving objects are copied to S1. The new generation of garbage collection is simple, crude and efficient.
    2. If the survivor area is found to be full, the objects are copied to the old area or the survivor is not full but some objects are old and are copied to the old area (each young GC will cause the survivor to survive the value of +1 until the threshold). Garbage collection also occurs in 3.OLD (young GC), where a Major GC is accompanied by at least one young GC, typically 10 times times slower than the young GC.
    3. The JVM does not have memory in the old area and will perform full GC. Old area uses the Concurrent-mark–sweep policy generally to reclaim memory.

Summary: The Java garbage collector is a "self-adaptive, generational, stop-copy, tag-sweep" garbage collector.

Disadvantages:

    1. STW (Stop-the-world) occurs during GC, and STW is time consuming if there are too many objects in the old area.

    2. The CMS collector is sensitive to CPU resources.

    3. The CMS collector is unable to handle floating garbage, and a "Concurrent Mode Failure" failure may occur resulting in another full GC.

    4. The CMS causes a memory fragmentation issue.

G1 Collector

In G1, the heap is divided into a number of contiguous regions (region). Each area is equal in size, between 1m~32m. The JVM supports up to 2000 zones, and the maximum memory supported by G1 can be estimated as 2000*32m=62.5g. The size of the region is determined by the JVM initialization, or it can be set with-xx:g1heapreginsize.

There is no physical Yong (eden/survivor)/old Generation in G1, they are logical and are made up of discontinuous regions (region).

New Generation Collection

G1 's Cenozoic collection is similar to that of Parnew, where the new generation takes up a certain percentage of time to start collecting.

The circled Green section is the Cenozoic region, and after young GC, the surviving objects are copied to one or more areas for free, and these filled areas will be new Cenozoic; When the age of the Cenozoic object (the age at which the young GC has escaped once increased by 1) has reached a certain threshold ( Parnew default 15), is copied to the region of the old age.

The recycling process is stalled (Stw,stop-the-word), and the size of Eden and survivor is adjusted according to the statistics of young GC, which helps to make reasonable use of memory and improve recovery efficiency.

The process of recycling multiple collection threads is collected concurrently.

old Age Collection

Similar to CMS, the G1 collector collects an old age object with a short pause.

    1. Mark the stage, first the initial tag (Initial-mark), which is paused (Stop the World Event), and triggers a normal mintor GC. corresponding GC log:gc pause (Inital-mark)

    2. Root region scanning, when the program runs, it recycles the survivor area (surviving to the old age), which must be done before young GC.

    3. Concurrent marking, which performs concurrency tagging throughout the heap (and application concurrency), this process can be interrupted by young GC. In the concurrency tagging phase, if all objects in a zone object are found to be garbage, the area is immediately recycled (x in the figure). At the same time, the object activity of each region (the proportion of the surviving objects in the region) is calculated during the concurrent tagging process.

    4. Remark, again marked, there will be a brief pause (STW). The re-tagging phase is used to collect the concurrency tag phase to generate new garbage (concurrent phases and applications run together), and the G1 uses a faster initial snapshot algorithm than the CMS: Snapshot-at-the-beginning (SATB).

    5. Copy/clean up, multi-threaded removal of inactivated objects, there will be STW. G1 copies the surviving objects of the reclaimed area to the new zone, clears the remember sets, and empties the Reclaim area and returns it to the list of idle areas.

    6. After the copy/purge process. The active object of the reclaimed area has been concentrated back into the deep blue and dark green areas.

about the remembered set concept: the object reference between the region in the G1 collector, and the Cenozoic and older generations in other collectors, uses the remembered set to avoid scanning the entire heap. Each region in the G1 has a corresponding remembered Set, and when the Virtual machine Discovery program writes to reference type data, a write barrier is temporarily interrupted. Check to see if the object referenced by reference is in a different region (in the example of generational, check whether an object in the old age refers to a new generation of objects), If so, the relevant reference information is recorded in the remembered set of the region to which the referenced object belongs, by Cardtable. When memory is reclaimed, adding remembered set at the enumeration scope of the GC root node guarantees that no global heap scan will be missed.

G1 retains the concept of a CMS, but the generation is not a physically contiguous area, but a logical concept. During the tagging process, the object activity of each region is calculated, and at the time of collection, it is possible to select areas of low activity based on user-set pause times, which can guarantee garbage collection and pause time, and will not degrade too much throughput. The application of the new algorithm in the remark stage and the compression in the collection process make up the shortage of CMS. Quote from Oracle's official website: "G1 is planned as the long term replacement for the Concurrent Mark-sweep Collector (CMS)".

Reference:

    1. Memory Management in the Java HotSpot? Virtual Machine

    2. Getting Started with the G1 garbage Collector

    3. Garbage-priority garbage collector tuning

    4. Deep understanding of Java Virtual machines: JVM advanced features and best practices

G1 garbage collector in the JVM

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.