写点什么

G1 收集器概述

作者:Nick
  • 2022 年 6 月 13 日
  • 本文字数:2241 字

    阅读完需:约 7 分钟

G1收集器概述

G1: The Garbage-First Collector, 垃圾回收优先的垃圾回收器,目标是用户多核 cpu 和大内存的机器,最大的特点就是可预测的停顿时间,官方给出的介绍是提供一个用户在大的堆内存情况下一个低延迟表现的解决方案,通常是 6GB 及以上的堆大小,有低于 0.5 秒稳定的可预测的停顿时间。


这里主要介绍这个比较新的垃圾回收器,在 G1 之前的垃圾回收器都是基于如下图的内存结构分布,有新生代,老年代和永久代(jdk8 之前),然后 G1 往前的那些垃圾回收器都有个分代,比如 serial,parallel 等,一般有个应用的组合,最初的 serial 和 serial old,因为新生代和老年代的收集方式不太一样,新生代主要是标记复制,所以有 eden 跟两个 survival 区,老年代一般用标记整理方式,而 G1 对这个不太一样。



看一下 G1 的内存分布



可以看到这有很大的不同,G1 通过将内存分成大小相等的 region,每个 region 是存在于一个连续的虚拟内存范围,对于某个 region 来说其角色是类似于原来的收集器的 Eden、Survivor、Old Generation,这个具体在代码层面


 // We encode the value of the heap region type so the generation can be  // determined quickly. The tag is split into two parts:  //  //   major type (young, old, humongous, archive)           : top N-1 bits  //   minor type (eden / survivor, starts / cont hum, etc.) : bottom 1 bit  //  // If there's need to increase the number of minor types in the  // future, we'll have to increase the size of the latter and hence  // decrease the size of the former.  //  // 00000 0 [ 0] Free  //  // 00001 0 [ 2] Young Mask  // 00001 0 [ 2] Eden  // 00001 1 [ 3] Survivor  //  // 00010 0 [ 4] Humongous Mask  // 00100 0 [ 8] Pinned Mask  // 00110 0 [12] Starts Humongous  // 00110 1 [13] Continues Humongous  //  // 01000 0 [16] Old Mask  //  // 10000 0 [32] Archive Mask  // 11100 0 [56] Open Archive  // 11100 1 [57] Closed Archive  //  typedef enum {    FreeTag               = 0,
YoungMask = 2, EdenTag = YoungMask, SurvTag = YoungMask + 1,
HumongousMask = 4, PinnedMask = 8, StartsHumongousTag = HumongousMask | PinnedMask, ContinuesHumongousTag = HumongousMask | PinnedMask + 1,
OldMask = 16, OldTag = OldMask,
// Archive regions are regions with immutable content (i.e. not reclaimed, and // not allocated into during regular operation). They differ in the kind of references // allowed for the contained objects: // - Closed archive regions form a separate self-contained (closed) object graph // within the set of all of these regions. No references outside of closed // archive regions are allowed. // - Open archive regions have no restrictions on the references of their objects. // Objects within these regions are allowed to have references to objects // contained in any other kind of regions. ArchiveMask = 32, OpenArchiveTag = ArchiveMask | PinnedMask | OldMask, ClosedArchiveTag = ArchiveMask | PinnedMask | OldMask + 1 } Tag;
复制代码


hotspot/share/gc/g1/heapRegionType.hpp


当执行垃圾收集时,G1 以类似于 CMS 收集器的方式运行。 G1 执行并发全局标记阶段,以确定整个堆中对象的存活性。标记阶段完成后,G1 知道哪些 region 是基本空的。它首先收集这些 region,通常会产生大量的可用空间。这就是为什么这种垃圾收集方法称为“垃圾优先”的原因。顾名思义,G1 将其收集和压缩活动集中在可能充满可回收对象(即垃圾)的堆区域。 G1 使用暂停预测模型来满足用户定义的暂停时间目标,并根据指定的暂停时间目标选择要收集的区域数。


由 G1 标识为可回收的区域是使用撤离的方式(Evacuation)。 G1 将对象从堆的一个或多个区域复制到堆上的单个区域,并在此过程中压缩并释放内存。撤离是在多处理器上并行执行的,以减少暂停时间并增加吞吐量。因此,对于每次垃圾收集,G1 都在用户定义的暂停时间内连续工作以减少碎片。这是优于前面两种方法的。 CMS(并发标记扫描)垃圾收集器不进行压缩。 ParallelOld 垃圾回收仅执行整个堆压缩,这导致相当长的暂停时间。


需要重点注意的是,G1 不是实时收集器。它很有可能达到设定的暂停时间目标,但并非绝对确定。 G1 根据先前收集的数据,估算在用户指定的目标时间内可以收集多少个区域。因此,收集器具有收集区域成本的合理准确的模型,并且收集器使用此模型来确定要收集哪些和多少个区域,同时保持在暂停时间目标之内。


注意:G1 同时具有并发(与应用程序线程一起运行,例如优化,标记,清理)和并行(多线程,例如 stw)阶段。Full GC 仍然是单线程的,但是如果正确调优,您的应用程序应该可以避免 Full GC。


在前面那篇中在代码层面简单的了解了这个可预测时间的过程,这也是 G1 的一大特点。


本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 署名 4.0 国际 (CC BY 4.0)

本文作者: Nicksxs

创建时间: 2020-02-09

本文链接: G1收集器概述

发布于: 刚刚阅读数: 4
用户头像

Nick

关注

还未添加个人签名 2017.12.22 加入

写代码的阿森 https://nicksxs.me https://nicksxs.com 也可以看我的博客

评论

发布
暂无评论
G1收集器概述_Java_Nick_InfoQ写作社区