zoukankan      html  css  js  c++  java
  • The Garbage-First (G1) collector

    The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. The G1 garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is designed for applications that:

    • Can operate concurrently with applications threads like the CMS collector.
    • Compact free space without lengthy GC induced pause times.
    • Need more predictable GC pause durations.
    • Do not want to sacrifice a lot of throughput performance.
    • Do not require a much larger Java heap.

    -XX:+UseG1GC        开启G1垃圾收集器
    -XX:MaxGCPauseMillis           目标停顿时间
    -XX:+PrintGCDetails       打印GC日志
    -XX:+PrintGCDateStamps    打印GC时间戳
    -XX:ParallelGCThreads   STW期间,并行GC线程数


     G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues(碎片问题). Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets(设置预期停顿时间).

     

    When performing garbage collections, G1 operates in a manner similar to the CMS collector. G1 performs a concurrent global marking phase to determine the liveness of objects throughout the heap. After the mark phase completes, G1 knows which regions are mostly empty. It collects in these regions first, which usually yields a large amount of free space. This is why this method of garbage collection is called Garbage-First. As the name suggests, G1 concentrates its collection and compaction activity on the areas of the heap that are likely to be full of reclaimable objects, that is, garbage. G1 uses a pause prediction model to meet a user-defined pause time target and selects the number of regions to collect based on the specified pause time target.

    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.

    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.

    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.

    Recommended Use Cases for G1

    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)

    The G1 Garbage Collector Step by Step


    In summary, the following can be said about the young generation in 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.
    • The young GC is done in parallel using multiple threads.
    • Live objects are copied to new survivor or old generation regions.

    young gc需要stop the world。采用多线程并行垃圾回收。G1是不提供full GC的。G1提供了两种GC模式,Young GCMixed GC,两种都是完全Stop The World的。Mixed GC不是full GC,它只能回收部分老年代的Region

    Global concurrent marking,它的执行过程类似CMS,但是不同的是,在G1 GC中,它主要是为Mixed GC提供标记服务的,并不是一次GC过程的一个必须环节。global concurrent marking的执行过程分为四个步骤:

    • 初始标记(initial mark,STW)。它标记了从GC Root开始直接可达的对象。
    • 并发标记(Concurrent Marking)。这个阶段从GC Root开始对heap中的对象标记,标记线程与应用程序线程并行执行,并且收集各个Region的存活对象信息。
    • 最终标记(Remark,STW)。标记那些在并发标记阶段发生变化的对象,将被回收。
    • 清除垃圾(Cleanup)。清除空Region(没有存活对象的),加入到free list

    各种垃圾收集器的对比:

    reference:

    https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

    https://tech.meituan.com/2016/09/23/g1.html

    转载请注明出处:https://www.cnblogs.com/lnas01/p/10348955.html

  • 相关阅读:
    SQL Server索引进阶:第九级,读懂执行计划
    SQL Server索引进阶:第八级,唯一索引
    SQL Server索引进阶:第七级,过滤的索引
    SQL Server索引进阶:第六级,标签
    SQL Server索引进阶:第五级,包含列
    SQL Server索引进阶:第四级,页和区
    SQL Server索引进阶:第三级,聚集索引
    SQL Server索引进阶:第二级,深入非聚集索引
    SQL Server索引进阶:第一级,索引简介
    2018 – 2019 年前端 JavaScript 面试题
  • 原文地址:https://www.cnblogs.com/lnas01/p/10348955.html
Copyright © 2011-2022 走看看