Java 8 G1

Source: Internet
Author: User
Tags diff

The G1 full name is garbage the garbage Collector, which uses G1 to simplify the complexities of performance tuning. For example, the primary input parameters for G1 are initialization and maximum Java heap size, and maximum GC interrupt time.

The G1 GC is made up of young generation and old generation. G1 the Java heap space into several region, that is, the young generation/old age is a series of region collections, which means that there is no need for a contiguous memory interval when allocating space, that is, it is not necessary to decide which region belong to the old age and which belong to the younger generation when the JVM starts. Because over time, young generations of Region are recycled and become available (unused Region or available Region, which is later mentioned).

The G1 young generation collector is a parallel stop-the-world collector, like other hotspot GC, when a young generation of GC occurs, the entire young generation is recycled. G1 's old age collector is different, it does not need the whole old age to reclaim in the old age, only part of region is called.

The young generation of G1 GC consists of Eden region and survivor region. When a JVM allocates Eden region fails, it triggers a young generation to recycle, meaning the Eden Zone is full. Then the GC begins to free up space, and the first young generation collector moves all the storage objects from Eden Region to Survivor Region, which is the "Copy to Survivor" process.

As shown in Listing 1, the collection GC output log for the younger generation, in this log, see the last line, the young generation of new size is 224 +32 (new Survivor) =256MB

Listing 1 G1 Recycling young generation

15.910: [GC Pause (Young), 0.0260410 secs]

[Parallel time:18.0 MS, GC Workers:4]

[GC Worker Start (ms): min:15909.7, avg:15909.7, max:15909.7, diff:0.0]

[Ext Root Scanning (ms): min:5.6, avg:6.1, max:6.8, diff:1.2, sum:24.3]

[Update RS (ms): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.1]

[Processed buffers:min:0, avg:12.5, max:24, diff:24, sum:50]

[Scan RS (ms): min:0.0, avg:0.1, max:0.1, diff:0.1, sum:0.3]

[Code Root Scanning (ms): min:0.0, avg:0.0, max:0.1, diff:0.1, sum:0.1]

[Object Copy (ms): min:11.1, avg:11.8, max:12.2, diff:1.1, sum:47.1]

[Termination (MS): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.0]

[GC Worker Other (ms): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.1]

[GC Worker Total (ms): min:18.0, avg:18.0, max:18.0, diff:0.0, sum:72.0]

[GC Worker End (ms): min:15927.7, avg:15927.7, max:15927.7, diff:0.0]

[Code Root fixup:0.1 MS]

[Code Root migration:0.2 MS]

[Clear ct:0.1 Ms]

[other:7.6 MS]

[Choose cset:0.0 MS]

[Ref proc:7.1 MS]

[Ref enq:0.1 MS]

[Free cset:0.2 MS]

[Eden:256.0m (256.0M)->0.0b (224.0M) survivors:0.0b->32.0m heap:256.0m (1024.0M)->35.1m (1024.0M)]

[times:user=0.06 sys=0.02, real=0.02 secs]

When a Java heap space bottleneck is exceeded, that is, the heap space is depleted, then G1 initializes the old age collector, a initial-mark phase that is parallel stop-the-world, and its size is related to the old age and the size of the entire Java heap.

The Initial-mark phase is executed with the next young GC, and once the Initial-mark completes, a multithreaded parallel marking phase begins to mark all surviving objects in the old age. When the parallel marking phase is complete, a parallel stop-the-world phase begins to mark all objects (application multithreaded programs may lose data due to the parallel existence of the marking phase). After the end of the remark node, G1 has complete tag information for all region in the old age, and if there is no task-surviving object in the old age, no additional GC work will be required for the next phase of the cleanup phase.

The above description is a description of the process of the old generation collector, a brief description of initial-mark-> Concurrent Root Region scanning->concurrent marking-> remarking- >cleanup.

In the cleanup phase, if the G1 GC discovers a recyclable region, do not wait for the next GC pause to recycle the region directly and add to the region list of the free LinkedList.

The CMS, Parallel, and serial GC all need to compress the old age through the full GC and scan the entire old age in this process.

Because the G1 operation is based on region, it applies to the large Java heap. Even if the Java heap is large, a large amount of GC work can be limited to small region collections. G1 allows the user to specify a pause-time target, G1 to meet this goal by adaptive heap size.

G1 GC Depth principle

G1 divides the entire Java heap into several intervals (regions). Each region size is a multiple of 2, ranging between 1MB-32MB and possibly 1,2,4,8,16,32mb. All region have the same size and will not change in the JVM lifecycle. For example-xmx16g–xms16g, set 16GB heap Size, 2000 regions, then each REGION=16GB/2000=8MB. If the heap size is large and the size of each region is small, the number of region may be more than 2000. Similarly, a small heap size can cause region to be low in number.

Region type

G1 several definitions of region:

Available region= Available Idle Region

Eden Region = Young Generation Eden Space

Suivivor region= young generation survivor space

The collection of all Eden and Survivor = the whole young generation

Humongous region= Large Object Region

Humongous Region

A large object is an object that occupies more than a region50% space, and this size contains the Java object header. The object header size differs between 32-bit and 64-bit hotspot VMS, and the Java Object Layout tool can be used to determine the header size, referred to as Jol.

When large objects begin to enter the queue, G1 will transfer several consecutive valid region to store it. The first Region is called "Big object Start" Region, the other regions is called "big object Continuation" regions. If no contiguous REGIONS,G1 is available, a full GC of the Java heap will be made to compress the object. The large object range is part of the old age, and it contains only one object, which allows G1 to collect a large object range when the parallel marking phase discovers that no object exists. When this condition is triggered, all intervals that contain large objects can be immediately recalled.

For G1, a potential challenge is that large objects in short life cycles may not be declared until they become not referenced. Jdk8u45 affirmed a method of recycling large object region in the younger generation.

As mentioned earlier, G1 Region includes young Region, old Region, humougous Region and free Region. Each collector unit spans a young and old regions. A large object spans two collector units, so the large object region is a continuous region, as shown in Figure 1.

Fig. 1 Region Cross distribution map 1

The next successive region is called continues Humongous. The large object 2 spans three successive heap regions, and the large object 3 spans a region.

Fig. 2 Region Cross distribution map 2

RSet

Old-age collectors use different areas of the heap to differentiate/isolate objects, and the objects within these different regions correspond to different eras. This time collectors can focus on the most recently allocated objects because they will find that some objects will soon die. These eras can be collected separately in the heap so that without scanning the entire heap, you can save time and reduce response time, and objects with long surviving time do not replicate back and forth, reducing the cost of copying and referencing updates.

To facilitate the independence of collectors, many GCs maintain the rset of each age. Each rset is a data structure that maintains and tracks internal references to the Collector unit, such as the region of the G1 GC, which reduces the time it takes to scan the entire heap heap for information. When the G1 GC performs a Stop-the-world collection (young generation or hybrid generation), it can scan region rsets via Cset. Once the surviving objects have been removed, their references are updated immediately.

Figure 3 RSet Layout diagram example

From the above Figure 3, we can see a young generation of Region (Region X) and two old age Region (Region y and Region Z). Region X has a reference from Region Z. This reference is marked in the RSet of Region X. We also observed that region Z has two references, one from region X and the other from region Y. Region Z's rset need to mark references from Region y, but there is no need to memorize references from Region X because the young generation is globally collected. For region y, we can finally see the references from Region X, and we don't record references in region y RSet.

Mixed GC Event

As many objects are promoted to the old age, and large objects enter a large object range, the entire Java heap area shares rise. To avoid Java heap space overflow, the JVM process needs to initialize a GC (not only the young generation regions, but also the older generation region to the hybrid collector). In a mixed GC event, all young generations of regions are collected, while some older region are collected.

Listing 2 G1 old age Recycling

120.298: [GC pause (mixed), 0.0221810 secs]

[Parallel time:17.1 MS, GC Workers:4]

[GC Worker Start (ms): min:120298.2, avg:120298.2, max:120298.2, diff:0.1]

[Ext Root Scanning (ms): min:5.6, avg:5.8, max:5.8, diff:0.2, sum:23.1]

[Update RS (ms): min:0.7, avg:0.9, max:1.0, diff:0.3, sum:3.5]

[Processed Buffers:min:4, avg:10.5, max:23, diff:19, sum:42]

[Scan RS (ms): min:1.1, avg:1.1, max:1.2, diff:0.1, sum:4.5]

[Code Root Scanning (ms): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.0]

[Object Copy (ms): min:9.0, avg:9.2, max:9.5, diff:0.4, sum:36.8]

[Termination (MS): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.0]

[GC Worker Other (ms): min:0.0, avg:0.0, max:0.0, diff:0.0, sum:0.1]

[GC Worker Total (ms): min:16.9, avg:17.0, max:17.0, diff:0.1, sum:68.0]

[GC Worker End (ms): min:120315.2, avg:120315.2, max:120315.2, diff:0.0]

[Code Root fixup:0.1 MS]

[Code Root migration:0.1 MS]

[Clear ct:0.1 Ms]

[other:4.7 MS]

[Choose cset:0.1 MS]

[Ref proc:4.1 MS]

[Ref enq:0.0 MS]

[Free cset:0.3 MS]

[Eden:224.0m (224.0M)->0.0b (224.0M) survivors:32.0m->32.0m heap:547.5m (1024.0M)->297.3m (1024.0M)]

[times:user=0.08 sys=0.00, real=0.02 secs]

Full garbage collections

Inside the G1, the hybrid GC mentioned above is a very important release memory mechanism that avoids G1 occurrences where region is not available or triggers a full GC event.

The CMS, Parallel, and serial GC all need to compress the old age through the full GC and scan the entire old age in this process. The full GC algorithm for G1 is exactly the same as the serial GC collector. When a full GC occurs, the entire Java heap performs a complete compression, which ensures that the maximum amount of free memory is available. G1 's full GC is a single-threaded, which can cause a long pause time, and the G1 design goal is to reduce the full GC to meet the applied energy target.

G1 GC Common parameters

The default values for the parameters I've listed here are all based on jdk8u45, so it's possible that subsequent JDK versions will have different values, and this reader can get the latest default information directly from the JDK official help document.

-XX:+USEG1GC: Enable G1 GC. JDK7 and JDK8 requirements must display the request to start G1 GC,JDK may set the G1 GC as the default GC option, or it may retreat to the early parallel GC, this also please pay attention to, JDK9 is expected to officially release 2017;

-xx:g1newsizepercent: The initial young generation occupies the entire Java heap size, the default value is 5%;

-xx:g1maxnewsizepercent: The largest young generation accounted for the size of the entire Java heap, the default value is 60%;

-xx:g1heapregionsize: Set the size of each region, MB, need to be 1,2,4,8,16,32 one, default is heap memory 1/2000. Before we talked about the concept of large objects, if this value is set relatively large, then large objects can enter the region, the same, the disadvantage of doing this is directly interfering with the distribution of all ages;

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.