G1垃圾收集器系统化说明【官方解读】

还是继续G1官网解读,上一次已经将这三节的东东读完了,如下:

所以接一来则继续往下读:

Reviewing Generational GC and CMS【回顾一下CMS收集器】

The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause collector) collects the tenured generation. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.

解读:

“The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause collector) collects the tenured generation. ”

并发标记清除(CMS)收集器(也叫做低延迟的收集器)收集的是老年代。

“It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. ”

它会尝试着将垃圾收集的停顿时间降到最低,是通过将垃圾收集工作与应用线程并发的执行。

“Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.”

通常情况下低延迟的并发收集器并不会复制或者压缩存活的对象。垃圾的收集是在不移动存活的对象就得到了完成。如果碎片化已经成为了一个问题,请分配 一个最大的堆。

Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.

说明:在年轻代的CMS收集器跟之前的收集器用的是同样的算法。

 
 
 

CMS Collection Phases【CMS收集的阶段】

The CMS collector performs the following phases on the old generation of the heap:
CMS收集器在堆的老年代会完成如下几个阶段:

这些都在之前完整学习过了,如下:

所以这里就不详述了,下面来复习一下。

Reviewing Garbage Collection Steps

回顾垃圾回收的步骤

Next, let's review CMS Collector operations step by step.

接下来,让我们来逐步回顾一下CMS收集器的操作。

Heap Structure for CMS Collector

CMS收集器堆结构

The heap is split into three spaces.

整个堆会被划分为三个区域。

Young generation is split into Eden and two survivor spaces. Old generation is one contiguous space. Object collection is done in place. No compaction is done unless there is a full GC.

年轻代会被划分为Eden和两个survivor空间,老年代是一个连续的空间。对象回收是就地完成的【就地完成的意思就是进行完垃圾回收也不会对其进行对象移动】。 不会进行压缩,除非进行了Full GC。

How Young GC works in CMS

在CMS中年轻代的使用方式

The young generation is colored light green and the old generation in blue. This is what the CMS might look like if your application has been running for a while. Objects are scattered around the old generation area.

年轻代被标记成了绿色,而老年代被标记成了蓝色。这是当你的应用程序运行一段之后这个CMS可能呈现的样子。对象在老年代中是分散的【因为没有压缩的过程】。

 

With CMS, old generation objects are deallocated in place. They are not moved around. The space is not compacted unless there is a full GC.

利用CMS,老年代的对象会就地进行回收。他们并不会移动。空间也不会进行压缩,除非进行了Full GC。

Young Generation Collection

年轻代的收集

Live objects are copied from the Eden space and survivor space to the other survivor space. Any older objects that have reached their aging threshold are promoted to old generation.

存活的对象会会从Eden空间和一个survivor空间复制到另一个survivor空间里。任何更老的对象如果已经达到了它们的阈值的话则会被晋升到老年代。

 

After Young GC

年轻代GC之后

After a young GC, the Eden space is cleared and one of the survivor spaces is cleared.

在年轻代完成GC后,Eden空间就会被清除掉,并且其中一个survivor空间也会被清除掉。

Newly promoted objects are shown in dark blue on the diagram. The green objects are surviving young generation objects that have not yet been promoted to old generation.

新晋升的对象在图中是用深蓝色来表示的。绿色对象是依然在年轻代存活着的还没被晋升到老年代。

Old Generation Collection with CMS

在CMS中老年代垃圾收集

Two stop the world events take place: initial mark and remark. When the old generation reaches a certain occupancy rate, the CMS is kicked off.

两个STW事件会发生:初始标记和重新标记。当老年代达到一定程度的占用率的话,CMS就会开始啦。

(1) Initial mark is a short pause phase where live (reachable) objects are marked. (2) Concurrent marking finds live objects while the application continues to execute. Finally, in the (3) remark phase, objects are found that were missed during (2) concurrent marking in the previous phase.

(1)初始标记会有一小段的暂时时间从而可到达存活的对象会被标记出来。(2)当应用继续执行时并发标记会找到存活的对象。最终在第三步重新标记阶段,在之前并发标记阶段如果对象没被找到则会重新的进行寻找。

Old Generation Collection - Concurrent Sweep

老年代收集---并发清除

Objects that were not marked in the previous phase are deallocated in place. There is no compaction.

在之前的阶段如果对象木有被就地标记,这是不会进行压缩的。

 

Note: Unmarked objects == Dead Objects

说明:没有被标记的对象就是消亡的对象

Old Generation Collection - After Sweeping

老年代收集---清除后

After the (4) Sweeping phase, you can see that a lot of memory has been freed up. You will also notice that no compaction has been done.

在第四个清除阶段结束之后,你可以发现有大量内存被释放掉了。你还可以注意到这里面未进行任何的压缩。

 

Finally, the CMS collector will move through the (5) resetting phase and wait for the next time the GC threshold is reached.

最后,CMS收集器会在第5重置阶段进行移动,并且会等到下一次GC的阈值被到达。

以上是对CMS垃圾回收器步骤的回顾,接下来则到重点啦:

The G1 Garbage Collector Step by Step

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

G1收集器采取的方式来分配堆空间。以下的图片一步步描述的G1垃圾收集系统到底是如何工作。 

G1 Heap Structure

G1堆结构

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.

区域的大小是在JVM启动时来指定的,JVM通常会划分成2000个区域,而每个区域的大小是从1到32M。

G1 Heap Allocation

G1堆的分配

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

实际上,这些区域会被映射成Eden、Survivor、老年代逻辑上的表示。

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.

如展示的区域会被分成Eden、survivor和老年代,此外,还有第四种类型的对像称之为“Humongous regions【巨大的区域】”,如果这些区域所持有的对象大小超出标准区域的50%或更大,则这些区域就称之为 “Humongous regions【巨大的区域】”。它们会被存储为一个连续的区域当中。最后一种类型就是未使用的堆区域。

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

说明:在写这篇文章时,收集大对象还没有优化,因此,你应该避免创建这种尺寸的大对象。

Young Generation in G1

在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.

整个堆大约会被分成2000个区域,最小区域的大小是1M,最大区域的大小是32M。蓝色区域持有老年代的对象,绿色区域持有的是年轻代的对象。 

Note that the regions are not required to be contiguous like the older garbage collectors.

注意:像老年代的区域是不要求要连续【而像以前的老垃圾收集器肯定是要求连续的】。

A Young GC in G1

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.

存活对象会被复制或移动到一个或者更多的survivor区域。如果这个阈值被满足了,其中的一些对象就会晋升到老年代中。【总体这块跟传统的垃圾收集器差不多】

 

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.

这里会出现STW的暂停。Eden和survivor的大小会被计算来供下一个年轻代的GC所使用。整体的信息会被保存起来来计算大小。暂停时间目标的这个事就会被考虑进来。

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

这种方式使得我们很容易的来调整区域的大小,使得它们根据实际需要变得更大或者更小。

End of a Young GC with G1

G1当中新生代GC的结束

Live objects have been evacuated to survivor regions or to old generation regions.

存活对象会被复制或者移动到survivor空间或者是老年代当中。 

Recently promoted objects are shown in dark blue. Survivor regions in green.

最近最晋升的对象是用蓝色标识的,Survivor空间是绿色。

In summary, the following can be said about the young generation in G1:
总结一下,下面展示了在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.
        新生代垃圾收集或者年轻代的GC,是会出现STW事件的。所有应用线程都会暂停下来以等待操作完成。
      • The young GC is done in parallel using multiple threads.
        新生代的GC是通过多线程的方式并行完成的。
      • Live objects are copied to new survivor or old generation regions.
        存活对象会被复制到新的survivor或者老年代区域中。

Old Generation Collection with G1
G1当中老年代收集

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.

就像CMS收集器一样,G1收集器是被设计成针对老年代的一个低延迟的收集器。下面表中描述了G1收集器在老年代中的阶段。

G1 Collection Phases - Concurrent Marking Cycle Phases
G1收集器的阶段--并发标记循环阶段

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.
G1收集器对于堆中老年代的收集会完成以下阶段。注意一些阶段也是新生代收集的部分。

Phase Description
(1) Initial Mark
(Stop the World Event)【初始标记,会出现STW】
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)【重新标记,会触发STW】
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)【清除,会触STW】
  • 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)].

G1 Old Generation Collection Step by Step

分步来看一下G1老年代的收集

With the phases defined, let's look at how they interact with the old generation in the G1 collector.
如阶段定义,让我们看一下在G1收集器中他们是如何作用于老年代中的。

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).

初使标记在年轻代垃圾收集器中存活的对象。在日志中会被标记成GC pause (young)(inital-mark)。

 

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.

如果找到了一些空分区的话(如图中用“X”来表示),它们就会从remark阶段立马移除。此外,“accouting”信息也会决定存活的计算。 

Remark Phase
重新标记阶段

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

空的区域会被移除和收回。区域的存活性会针对所有的区域进行计算。

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.

G1会选择拥有最低存活率的分区【也就是会找区域中存活对象最小,也就是回收的对象最多】,这些区域可以被回收的速度是最快的。那么这些区域同时会和年轻代GC一同进行收集。在日志当中是被标记成了“GC pause (mixed)”。因此新生代和老年代是同时间被收集的。

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

总结一下老年代的GC

In summary, there are a few key points we can make about the G1 garbage collection on the old generation.
总之,有一些关键的点我们可以了解在老年代上G1垃圾收集器。

      • 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.
          这里并没有像在CMS当中的清除阶段。
      • Remark Phase
        重新标记阶段
        • Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.
          用SATB算法它的速度要比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.
          老年代的区域会基于它们的存活性进行选择。

 好,此章节已经完全分析完了,还剩下如下几章:

由于是跟日志相关的东东,会在之后的实践用到的时候再来学习。

原文地址:https://www.cnblogs.com/webor2006/p/11142839.html