还是继续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.
          老年代的区域会基于它们的存活性进行选择。

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

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

G1垃圾收集器系统化说明【官方解读】的更多相关文章

  1. G1垃圾收集器官方文档透彻解读【官方解读】

    在前几次中已经对G1的理论进行了一个比较详细的了解了,对于G1垃圾收集器最权威的解读肯定得上官网,当咱们将官网的理解透了,那基本上网上对于G1的说明其实最终都是来自于官网,所以接下来会详细来解读Ora ...

  2. 深入理解 Java G1 垃圾收集器--转

    原文地址:http://blog.jobbole.com/109170/?utm_source=hao.jobbole.com&utm_medium=relatedArticle 本文首先简单 ...

  3. 【JVM】7、深入理解Java G1垃圾收集器

    本文首先简单介绍了垃圾收集的常见方式,然后再分析了G1收集器的收集原理,相比其他垃圾收集器的优势,最后给出了一些调优实践. 一,什么是垃圾回收 首先,在了解G1之前,我们需要清楚的知道,垃圾回收是什么 ...

  4. 详解 JVM Garbage First(G1) 垃圾收集器(转载)

    前言 Garbage First(G1)是垃圾收集领域的最新成果,同时也是HotSpot在JVM上力推的垃圾收集器,并赋予取代CMS的使命.如果使用Java 8/9,那么有很大可能希望对G1收集器进行 ...

  5. 转:深入理解Java G1垃圾收集器

    本文首先简单介绍了垃圾收集的常见方式,然后再分析了G1收集器的收集原理,相比其他垃圾收集器的优势,最后给出了一些调优实践. 一,什么是垃圾回收 首先,在了解G1之前,我们需要清楚的知道,垃圾回收是什么 ...

  6. [转] 深入理解Java G1垃圾收集器

    [From] https://www.cnblogs.com/ASPNET2008/p/6496481.html 深入理解Java G1垃圾收集器 本文首先简单介绍了垃圾收集的常见方式,然后再分析了G ...

  7. 深入理解Java G1垃圾收集器

    本文首先简单介绍了垃圾收集的常见方式,然后再分析了G1收集器的收集原理,相比其他垃圾收集器的优势,最后给出了一些调优实践. 一,什么是垃圾回收 首先,在了解G1之前,我们需要清楚的知道,垃圾回收是什么 ...

  8. G1垃圾收集器设计目标与改良手段【纯理论】

    在之前已经详细对CMS垃圾回收器进行了学习,今天准备要学习另一个全新的垃圾收集器---G1(Garbage First Collector 垃圾优先的收集器),说是一种全新的,其实G1垃圾收集器已经出 ...

  9. 关于生产环境改用G1垃圾收集器的思考

    背景 由于我们的业务量非常大,响应延迟要求高.目前沿用的老的ParNew+CMS已经不能支撑业务的需求.平均一台机器在1个月内有1次秒级别的stop the world.对系统来说是个巨大的隐患.所以 ...

随机推荐

  1. node excel export包导致find函数被覆盖

    这个包确实是巨坑:https://github.com/functionscope/Node-Excel-Export 本来是想用来导出Excel的,没想到把Array的find函数能乱了.这种基础函 ...

  2. npm 的一些命令

    查看项目中是否安装某个插件 npm [name] -v [name] 为要查询的插件的名字,如果已经安装就会显示该插件的版本号 npm list 查看项目中所有已安装的插件

  3. 【NPDP笔记】第二章 组合管理

    2.1 什么是产品组合 Product Portfolio 什么是组合管理,讲述的是完成正确的项目, 五大目标 财务稳健,财务目标 管道平衡,资源需求与可用资源之间的平衡 战略协同,与经营战略 组织战 ...

  4. WEBSHELL-恶意代码检测

    静态查杀 提取特征写成规则库,调用规则库查杀.基于规则,会比较快,但漏报.误报会比较明显,一般的Webshell一句话木马变形混淆会比较多. yara规则 $eval = /(<\?php|[; ...

  5. Java并发之等待/通知机制

    目录 1 前言 1.1 先来段代码放松一下 2 Object wait()/notify() 2.1 一段入门代码 2.2 问题三连击 a.为什么官方说wait() 要放在while里面? b.为什么 ...

  6. 一秒可生成500万ID的分布式自增ID算法—雪花算法 (Snowflake,Delphi 版)

    概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种 ...

  7. 测试代码的练习——python编程从入门到实践

    11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名.这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.这个函数存储在一个名为city_fu ...

  8. 【LeetCode】 #7:反转整数 C语言

    目录 题目 思路 初步想法 进一步想法 总结 最近打算练习写代码的能力,所以从简单题开始做. 大部分还是用C语言来解决. @(解法) 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数 ...

  9. John Lemon's Haunted Jaunt(鬼屋游戏笔记)

    1.使用Unity  2019.2.3 2.角色移动的控制脚本 3.后期处理组件PostProcessLayer  (类似给相机加上了一层滤镜) 4.制作简单的怪物AI系统,使用 NAvMeshAge ...

  10. python 2种创建多线程的方法

    多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度 # 第一种方法:将要执行的方法作为参数传给Thread的构造方法 import threading import t ...