当执行垃圾收集时,G1以类似于CMS收集器的方式运行。 G1执行并发全局标记阶段,以确定整个堆中对象的活动性。 标记阶段完成后,G1知道哪些区域大部分为空。 它首先收集在这些区域中,通常会产生大量的自由空间。 这就是为什么这种垃圾收集方法称为“垃圾优先”的原因。 顾名思义,G1将其收集和压缩活动集中在可能充满可回收对象(即垃圾)的堆区域。 G1使用暂停预测模型来满足用户定义的暂停时间目标,并根据指定的暂停时间目标选择要收集的区域数。
The regions identified by G1 as ripe for reclamation are garbage collected using evacuation. G1 copies objects from one or more regions of the heap to a single region on the heap, and in the process both compacts and frees up memory. This evacuation is performed in parallel on multi-processors, to decrease pause times and increase throughput. Thus, with each garbage collection, G1 continuously works to reduce fragmentation, working within the user defined pause times. This is beyond the capability of both the previous methods. CMS (Concurrent Mark Sweep ) garbage collector does not do compaction. ParallelOld garbage collection performs only whole-heap compaction, which results in considerable pause times.
由G1标识为可回收的成熟区域是使用疏散收集的垃圾。 G1将对象从堆的一个或多个区域复制到堆上的单个区域,并在此过程中压缩并释放内存。 撤离是在多处理器上并行执行的,以减少暂停时间并增加吞吐量。 因此,对于每个垃圾收集,G1都在用户定义的暂停时间内连续工作以减少碎片。 这超出了前面两种方法的能力。 CMS(并发标记扫描)垃圾收集器不进行压缩。 ParallelOld垃圾回收仅执行整个堆压缩,这导致相当长的暂停时间。
It is important to note that G1 is not a real-time collector. It meets the set pause time target with high probability but not absolute certainty. Based on data from previous collections, G1 does an estimate of how many regions can be collected within the user specified target time. Thus, the collector has a reasonably accurate model of the cost of collecting the regions, and it uses this model to determine which and how many regions to collect while staying within the pause time target.
重要的是要注意,G1不是实时收集器。 它很有可能达到设定的暂停时间目标,但并非绝对确定。 G1根据先前收集的数据,估算在用户指定的目标时间内可以收集多少个区域。 因此,收集器具有收集区域成本的合理准确的模型,并且收集器使用此模型来确定要收集哪些和多少个区域,同时保持在暂停时间目标之内。
Note: G1 has both concurrent (runs along with application threads, e.g., refinement, marking, cleanup) and parallel (multi-threaded, e.g., stop the world) phases. Full garbage collections are still single threaded, but if tuned properly your applications should avoid full GCs.
注意: G1同时具有并发(与应用程序线程一起运行,例如优化,标记,清理)和并行(多线程,例如,停止世界)阶段。 Full garbage collections仍然是单线程的,但是如果正确调整,您的应用程序应避免使用完整的GC。
G1 Footprint
If you migrate from the ParallelOldGC or CMS collector to G1, you will likely see a larger JVM process size. This is largely related to "accounting" data structures such as Remembered Sets and Collection Sets.
G1足迹
如果从ParallelOldGC或CMS收集器迁移到G1,则可能会看到较大的JVM进程大小。 这在很大程度上与“记帐”数据结构有关,例如“记住的集”和“集合集”。
Remembered Sets or RSets track object references into a given region. There is one RSet per region in the heap. The RSet enables the parallel and independent collection of a region. The overall footprint impact of RSets is less than 5%.
Collection Sets or CSets the set of regions that will be collected in a GC. All live data in a CSet is evacuated (copied/moved) during a GC. Sets of regions can be Eden, survivor, and/or old generation. CSets have a less than 1% impact on the size of the JVM.
记住集或RSets将对象引用跟踪到给定区域中。 堆中每个区域有一个RSet。 RSet允许并行和独立地收集区域。 RSets的总体足迹影响小于5%。
收集集或CS设置将在GC中收集的区域集。 GC期间,将撤消(复制/移动)CSet中的所有实时数据。 区域集可以是伊甸园,幸存者和/或老一辈。 CSets对JVM的大小影响不到1%。
Recommended Use Cases for G1