文档章节

The G1 Garbage Collector

秋风醉了
 秋风醉了
发布于 2015/11/19 19:01
字数 1286
阅读 136
收藏 0

The G1 Garbage Collector

The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. The G1 garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is designed for applications that:

  • Can operate concurrently with applications threads like the CMS collector.

  • Compact(压紧) free space without lengthy(冗长的) GC induced pause times.

  • Need more predictable(可预见的) GC pause durations.

  • Do not want to sacrifice(牺牲) a lot of throughput performance.

  • Do not require a much larger Java heap.

G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies(依赖于) on regions. This considerably simplifies parts of the collector, and mostly eliminates(消除) potential(潜在的,有可能的) fragmentation issues. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

 

The G1 collector takes a different approach to allocating the heap. The pictures that follow review the G1 system step by step.

G1 Young Generation Collection Step by Step

1.G1 Heap Structure

The heap is one memory area split into many fixed sized regions.

Region size is chosen by the JVM at startup. The JVM generally targets around 2000 regions varying in size from 1 to 32Mb.

 

2.G1 Heap Allocation

In reality, these regions are mapped into logical representations of Eden, Survivor, and old generation spaces.

The colors in the picture shows which region is associated with which role. Live objects are evacuated (i.e., copied or moved) from one region to another. Regions are designed to be collected in parallel with or without stopping all other application threads.

As shown regions can be allocated into Eden, survivor, and old generation regions. In addition, there is a fourth type of object known as Humongous regions. These regions are designed to hold objects that are 50% the size of a standard region or larger. They are stored as a set of contiguous regions. Finally the last type of regions would be the unused areas of the heap.

Note: At the time of this writing, collecting humongous objects has not been optimized. Therefore, you should avoid creating objects of this size.

 

3.Young Generation in G1

The heap is split into approximately 2000 regions. Minimum size is 1Mb and maximum size is 32Mb. Blue regions hold old generation objects and green regions hold young generation objects.

 

4.A Young GC in G1

Live objects are evacuated (i.e., copied or moved) to one or more survivor regions. If the aging threshold is met, some of the objects are promoted to old generation regions.

This is a stop the world (STW) pause. Eden size and survivor size is calculated for the next young GC. Accounting information is kept to help calculate the size. Things like the pause time goal are taken into consideration.

This approach makes it very easy to resize regions, making them bigger or smaller as needed.

 

5.End of a Young GC with G1

Live objects have been evacuated(撤离) to survivor regions or to old generation regions.

Recently promoted(提升) objects are shown in dark blue. Survivor regions in green.

In summary, the following can be said about the young generation in G1:

  • The heap is a single memory space split into regions.

  • Young generation memory is composed(组成) of a set of non-contiguous(接触的,临近的) regions. This makes it easy to resize when needed.

  • Young generation garbage collections, or young GCs, are stop the world events. All application threads are stopped for the operation.

  • The young GC is done in parallel using multiple threads.

  • Live objects are copied to new survivor or old generation regions.

 

G1 Old Generation Collection Step by Step

Like the CMS collector, the G1 collector is designed to be a low pause collector for old generation objects. The following table describes the G1 collection phases on old generation.

The G1 collector performs the following phases on the old generation of the heap. Note that some phases are part of a young generation collection.

With the phases defined, let's look at how they interact with the old generation in the G1 collector.

Phase Description
(1) Initial Mark
       (Stop the World Event)
This is a stop the world event. With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation.
(2) Root Region Scanning Scan survivor regions for references into the old generation. This happens while the application continues to run. The phase must be completed before a young GC can occur.
(3) Concurrent Marking Find live objects over the entire heap. This happens while the application is running. This phase can be interrupted by young generation garbage collections.
(4) Remark
      (Stop the World Event)
Completes the marking of live object in the heap. Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than what was used in the CMS collector.
(5) Cleanup
      (Stop the World Event and Concurrent)
  • Performs accounting on live objects and completely free regions. (Stop the world)

  • Scrubs(擦洗,净化) the Remembered Sets. (Stop the world)

  • Reset the empty regions and return them to the free list. (Concurrent)

(*) Copying
      (Stop the World Event)

These are the stop the world pauses to evacuate(撤离) or copy live objects to new unused regions. This can be done with young generation regions which are logged as [GC pause (young)]. Or both young and old generation regions which are logged as [GC Pause (mixed)].

With the phases defined, let's look at how they interact with the old generation in the G1 collector.

6.Initial Marking Phase

Initial marking of live object is piggybacked on a young generation garbage collection. In the logs this is noted as GC pause (young)(inital-mark).

 

7.Concurrent Marking Phase

If empty regions are found (as denoted by the "X"), they are removed immediately in the Remark phase. Also, "accounting" information that determines liveness is calculated.

 

8.Remark Phase

Empty regions are removed and reclaimed. Region liveness is now calculated for all regions.

 

9.Copying/Cleanup Phase

G1 selects the regions with the lowest "liveness", those regions which can be collected the fastest. Then those regions are collected at the same time as a young GC. This is denoted in the logs as [GC pause (mixed)]. So both young and old generations are collected at the same time.

 

10.After Copying/Cleanup Phase

The regions selected have been collected and compacted into the dark blue region and the dark green region shown in the diagram.

 

Summary of Old Generation GC

In summary, there are a few key points we can make about the G1 garbage collection on the old generation.

Concurrent Marking Phase

Liveness information is calculated concurrently while the application is running.

This liveness information identifies which regions will be best to reclaim during an evacuation pause.

There is no sweeping phase like in CMS.

Remark Phase

Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.

Completely empty regions are reclaimed.

Copying/Cleanup Phase

Young generation and old generation are reclaimed at the same time.

Old generation regions are selected based on their liveness.

========END========

本文转载自:http://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

共有 人打赏支持
秋风醉了
粉丝 244
博文 546
码字总数 414070
作品 0
朝阳
程序员
私信 提问
Java 8 - From PermGen to Metaspace

Java 8 - From PermGen to Metaspace http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html http://ifeve.com/java-permgen-removed/ https://blogs.oracle.com/poonam/abo......

秋风醉了
2014/10/26
1K
0
Tuning Java Garbage Collection for Spark Applicati

This is a guest post from our friends in the SSG STO Big Data Technology group at Intel. Join us at the Spark Summit to hear from Intel and other companies deploying Spark in pr......

kuerant
2015/05/30
0
1
G1 垃圾收集器介绍

原文出处:占小狼 为解决CMS算法产生空间碎片和其它一系列的问题缺陷,HotSpot提供了另外一种垃圾回收策略,G1(Garbage First)算法,通过参数来启用,该算法在JDK 7u4版本被正式推出,官网...

占小狼
2018/01/15
0
0
Java SE 6 Update 14 Early Access 早期使用版本发布

Java SE 6 Update 14 Early Access 早期使用版本现已发布,新更新功能包括: 为 Windows JRE 提供Service Tag 创建功能 :Sun Service Tag是产品识别码,用于自动探索Sun系统、软体及服务,以...

红薯
2009/03/19
586
0
JVM KnowLedge Collection

标记清除是JVM用于垃圾回收的基本算法 标记清除算法中,引用会从每个线程栈的桢指向程序的堆 从栈开始,循着指针找到所有可能的引用,然后再循着这些引用递归下去。 当递归完成,就找到了所有...

散关清渭
2014/03/01
0
0

没有更多内容

加载失败,请刷新页面

加载更多

指针数组和数组指针的区别

这两个名字不同当然所代表的意思也就不同。我刚开始看到这就吓到了,主要是中文太博大精深了,整这样的简称太专业了,把人都绕晕了。从英文解释或中文全称看就比较容易理解。 指针数组:arr...

天王盖地虎626
28分钟前
0
0
Qt那些事0.0.18

今天要记一下Qt中的Resource。自我感觉理解的不错,但是还会难免有谬误,在日后有可能会更新,也有可能不会。 小声的念叨一句,女人心,海底针。 今天就直接跳过了关于QML在qrc文件中的介绍,...

Ev4n
36分钟前
1
0
深入解析js的作用域、预解析机制

虽然,ES6在我们工作中应用得越来越广泛,但是还是很多项目保留着ES5的写法,所以,今天,带着大家重新巩固下ES5下的作用域及预解析机制。 概念: 作用域:域,指的是一个空间、范围、区域,...

前端攻城老湿
39分钟前
0
0
Spring Cloud Feign - 声明式 REST Client

1、Feign是什么 声明式REST client,来自NetFlix。 允许你编写无实现代码调用REST services 替换RestTemplate(甚至更简单) Spring Cloud 为使用Feign提供了包装器 2、怎样使用Feign 对比:...

Benz001
44分钟前
3
0
前端、后端和全栈到底不该学什么

1、前言 在职业规划咨询过程中经常会被问到这样的问题: 老师,我是该深入钻研专精一门,走技术大牛路线,还是所有都要精通,做一个全栈工程师? 类似问题的变种还有,老师我是不是该30岁最迟...

前端攻城小牛
46分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部