The first focus of G1 is to provide a solution for users running applications that require large heaps with limited GC latency. This means heap sizes of around 6GB or larger, and stable and predictable pause time below 0.5 seconds.
Applications running today with either the CMS or the ParallelOldGC garbage collector would benefit switching to G1 if the application has one or more of the following traits.
Full GC durations are too long or too frequent.
The rate of object allocation rate or promotion varies significantly.
Undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second)
Note: If you are using CMS or ParallelOldGC and your application is not experiencing long garbage collection pauses, it is fine to stay with your current collector. Changing to the G1 collector is not a requirement for using the latest JDK.
#### G1的推荐用例
G1的首要重点是为运行需要大堆且GC延迟有限的应用程序的用户提供解决方案。 这意味着堆大小约为6GB或更大,并且稳定且可预测的暂停时间低于0.5秒。
如果当前具有CMS或ParallelOldGC垃圾收集器运行的应用程序具有以下一个或多个特征,则将其切换到G1将非常有益。
-完整的GC持续时间太长或太频繁。
-对象分配率或提升率差异很大。
-不希望的长时间垃圾收集或压缩暂停(长于0.5到1秒)
注意:如果您使用的是CMS或ParallelOldGC,并且您的应用程序没有经历较长的垃圾收集暂停,那么可以继续使用当前的收集器。 使用最新的JDK不需要更改为G1收集器。
Reviewing Generational GC and 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.
Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.
回顾世代GC和CMS
并发标记扫描(CMS)收集器(也称为并发低暂停收集器)收集有期限的世代。 它尝试通过与应用程序线程同时执行大多数垃圾回收工作来最大程度地减少由于垃圾回收导致的暂停。 通常,并发的低暂停收集器不会复制或压缩活动对象。 无需移动活动对象即可完成垃圾回收。 如果碎片成为问题,请分配更大的堆。
注意:年轻一代的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系统。
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.
堆结构
堆是一个内存区域,分为多个固定大小的区域。
区域大小由JVM在启动时选择。 JVM通常针对大约2000个区域,大小从1到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.
图片中的颜色显示了哪个区域与哪个角色相关联。 将活动对象从一个区域撤离(即复制或移动)到另一个区域。 区域被设计为在不停止所有其他应用程序线程的情况下并行收集。
如图所示,可以将区域分配到Eden,幸存者和旧时代区域。 此外,还有第四种类型的物体被称为巨大区域。 这些区域旨在容纳标准区域大小的50%或更大的对象。 它们存储为一组连续区域。 最后,最后一种区域类型将是堆的未使用区域。
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.
Note that the regions are not required to be contiguous like the older garbage collectors.
堆分为大约2000个区域。 最小大小为1Mb,最大大小为32Mb。 蓝色区域保存旧对象,绿色区域保存年轻对象。
请注意,区域不需要像旧的垃圾收集器那样连续。
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.