The Garbage-First Garbage Collector (or G1 GC for short) is a new GC that is being introduced in the Java HotSpot VM in JDK 7. An experimental version of G1 has also been released in Java SE 6 Update 14. G1 is the long-term replacement for HotSpot's low-latency Concurrent Mark-Sweep GC (widely referred to as CMS).
Attributes
G1 is a “server-style” GC and has the following attributes.
-
Parallelism and Concurrency. G1 takes advantage of the parallelism that exists in hardware today. It uses all available CPUs (cores, hardware threads, etc.) to speed up its “stop-the-world” pauses when an application's Java threads are stopped to enable GC. It also works concurrently with running Java threads to minimize whole-heap operations during stop-the-world pauses.
-
Generational. Like the other HotSpot GC's, G1 is generational, meaning it treats newly-allocated (aka young) objects and objects that have lived for some time (aka old) differently. It concentrates garbage collection activity on young objects, as they are the ones most likely to be reclaimable, while visiting old objects infrequently. For most Java applications, generational garbage collection has major efficiency advantages over alternative schemes.
-
Compaction. Unlike CMS, G1 performs heap compaction over time. Compaction eliminates potential fragmentation problems to ensure smooth and consistent long-running operation.
-
Predictability. G1 is expected to be more predictable than CMS. This is largely due to the elimination of fragmentation issues that can negatively affect stop-the-world pause times in CMS. Additionally, G1 has a pause prediction model that, in many situations, allows it to often meet (or rarely exceed) a pause time target.
Description
G1 takes a very different approach to heap layout than other HotSpot GCs. In G1, there is no physical separation between the young and old generations. Instead, there is a single contiguous heap which is split into same-sized regions. The young generation is a set of potentially non-contiguous regions, and the same is true for the old generation. This allows G1 to flexibly move resources as needed from the old to the young generation, and vice versa.
Collection in G1 takes place through evacuation pauses, during which the survivors from a set of regions referred to as the collection set are evacuated to another set of regions (the to-space) so that the collection set regions can then be reclaimed. Evacuation pauses are done in parallel, with all available CPUs participating. Most evacuation pauses collect the available young regions, thus are the equivalent of young collections in other HotSpot GCs. Occasionally, select old regions may also be collected during these pauses because G1 piggybacks old generation collection activity on young collections.
Like CMS, G1 periodically performs a concurrent marking phase. The main role of this phase is to identify which old regions are mostly full of garbage objects, as these are the most efficient and desirable to collect. Unlike CMS, G1 does not perform a concurrent sweeping phase. Instead, the most profitable old regions identified by the concurrent marking phase are collected during subsequent evacuation pauses (the piggybacking mentioned above).
Using G1
G1 is still considered experimental and can be enabled with the following two parameters:
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC
To set a GC pause time goal, use the following parameter:
-XX:MaxGCPauseMillis =50
(for a pause time target of 50ms)
With G1, a time interval can be specified during which a GC pause should last no longer than the time given above:
-XX:GCPauseIntervalMillis =200
(for a pause interval target of 200ms)
Note that the above two options represent goals, not promises or guarantees. They might work well in some situations but not in others, and the GC might not always be able to obey them.
Alternatively, the size of the young generation can be specified explicitly to impact evacuation pause times:
-XX:+G1YoungGenSize=512m
(for a 512 megabyte young generation)
G1 also uses the equivalent of survivor spaces, which are, naturally, a set of (potentially non-contiguous) regions. Their size can be specified with the usual parameters (e.g., -XX:SurvivorRatio=6
).
Finally, to run G1 at its full potential, try setting these two parameters which are currently disabled by default because they may uncover a rare race condition:
-XX:+G1ParallelRSetUpdatingEnabled -XX:+G1ParallelRSetScanningEnabled
One more thing to note is that G1 is very verbose compared to other HotSpot GCs when -XX:+PrintGCDetails
is set. This is because it prints per-GC-thread timings and other information very helpful in profiling and trouble-shooting. If you want a more concise GC log, please switch to using -verbosegc
(though it is recommended that the more detailed GC log be obtained).
Status
G1 development is now focused primarily on resolving any remaining reliability issues and improving performance, and also on removing the following limitations:
- G1 does not fully support the JVM Tool Interface (JVM TI) or Java Management Extensions (JMX), so it is likely that monitoring and management tools will not work correctly with G1.
- G1 does not support incremental permanent generation collection. If an application does a lot of class unloading and requires permanent generation collection, this will be done during Full GCs.
- In terms of GC pause times, G1 is sometimes better and sometimes worse than CMS. Work is ongoing to make G1 consistently as good as, if not better than, CMS.
Resources
- Description of HotSpot GCs: Memory Management in the Java HotSpot Virtual Machine White Paper: http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf
- The original CMS paper: Printezis, T. and Detlefs, D. 2000. A generational mostly-concurrent garbage collector. In Proceedings of the 2nd international Symposium on Memory Management (Minneapolis, Minnesota, United States, October 15 - 16, 2000). http://portal.acm.org/citation.cfm?id=362422.362480 (requires access to ACM's portal)
- The original G1 paper: Detlefs, D., Flood, C., Heller, S., and Printezis, T. 2004. Garbage-first garbage collection. In Proceedings of the 4th international Symposium on Memory Management (Vancouver, BC, Canada, October 24 - 25, 2004). http://portal.acm.org/citation.cfm?id=1029879 (requires access to ACM's portal)
- G1 talk from JavaOne 2008: http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-5419&yr=2008
分享到:
相关推荐
Garbage-First is a server-style garbage collector, targeted for multi-processors with large memories, that meets a soft real-time goal with high probability, while achieving high throughput. Whole-...
4. Garbage-First(G1)垃圾收集器:G1垃圾收集器是Java 7中引入的新型垃圾收集器,使用增量式标记-清除算法。 5. Z垃圾收集器(Z Garbage Collector):Z垃圾收集器是Java 11中引入的低延迟垃圾收集器,使用增量式...
4. **垃圾收集器类型**:HotSpot提供了多种GC策略,如Serial GC、Parallel GC、Parallel Old GC、CMS(Concurrent Mark Sweep)、G1(Garbage-First)和ZGC(Zing Concurrent Low Latency Collector)。每种收集器有...
4. **垃圾收集器类型**:Java虚拟机(JVM)提供了多种垃圾收集器,如Serial、Parallel、Parallel Old、CMS(Concurrent Mark Sweep)、G1(Garbage-First)和ZGC(Zing Garbage Collector)。每种收集器有其特定的...
4. **G1收集器** (9 Garbage-First Garbage Collector .png & 10 Garbage-First Garbage Collector Tuning .png) - G1收集器是现代Java应用的首选,它引入了区域(Region)的概念,可以预测并控制垃圾收集的暂停...
6. 多区域收集器:文档进一步介绍了如垃圾优先(Garbage-First,G1)、平衡(Balanced)、调时器(Metronome)以及C4和Shenandoah收集器等多区域收集器,这些收集器为大规模应用提供了不同的性能特征。 7. 堆结构:...
4. G1(Garbage-First)Garbage Collector:G1是为大内存应用设计的,它将堆划分为多个区域,可以并行、增量地回收内存,同时最小化碎片。G1能够预测和控制垃圾回收停顿时间,适合大规模服务器应用。启用G1使用-XX:...
然而,某些阶段(如对象标记或空间整理)可能需要暂停应用,这就产生了“暂停时间”(Stop-the-World)。优化垃圾回收器的目标之一是尽可能缩短这些暂停时间,提高系统响应速度。 4. **垃圾回收策略**:不同的垃圾...
- ZGC(Z Garbage Collector)和Shenandoah:低暂停时间,适用于大规模数据处理。 3. 垃圾回收过程 - 标记:找出所有活动对象。 - 清除:删除未被标记的对象。 - 整理:移动存活对象,紧凑内存空间。 4. Stop-...
3. **垃圾收集器类型**:Java提供了多种不同的垃圾收集器,如Serial GC、Parallel GC、CMS(Concurrent Mark Sweep)、G1(Garbage-First)和ZGC(Zing Garbage Collector)。每种收集器在性能、停顿时间和资源消耗...
this book provides unprecedented detail on two powerful Java platform innovations: the Garbage First (G1) garbage collector and the HotSpot VM Serviceability Agent. Coverage includes Leveraging G1 ...
First, you'll learn the best practices for writing Ruby code that's easy not only on the CPU, but also on memory, and that doesn't trigger the dreaded garbage collector. You'll find out that garbage ...
First, you’ll learn the best practices for writing Ruby code that’s easy not only on the CPU, but also on memory, and that doesn’t trigger the dreaded garbage collector. You’ll find out that ...
3. **G1收集器(Garbage-First Garbage Collector)**:目标是可预测的暂停时间,适合大型应用,可以通过 `-XX:+UseG1GC` 开启。 4. **Z收集器(Z Garbage Collector)**:也称为ZGC,设计目标是极低的暂停时间,...
8. **并行压缩堆栈**:Java 7中的Parallel Old Garbage Collector(-XX:+UseParallelOldGC)引入了压缩堆栈,减少了内存碎片,提升了大应用的性能。 9. **动态语言支持**:Java 7添加了invokedynamic指令,为运行时...
3. **Concurrent Mark Sweep (CMS) GC**:这种垃圾回收器尝试并发地执行大部分垃圾回收操作,以减少"Stop-the-world"事件。它适合响应时间敏感的应用。 4. **G1 (Garbage-First) GC**:G1是一种并行和并发的垃圾...