摘录自:http://www.drdobbs.com/jvm/g1-javas-garbage-first-garbage-collector/219401061?pgno=1
Parallelism and Concurrency
When speaking about garbage collection algorithms, parallelism describes the collector's ability to perform its work across multiple threads of execution. Concurrency describes its ability to do work while application threads are still running. Hence, a collector can be parallel but not concurrent, concurrent but not parallel, or both parallel and concurrent.
The Java parallel collector (the default) is parallel but not concurrent as it pauses application threads to do its work. The CMS collector is parallel and partially concurrent as it pauses application threads at many points (but not all) to do its work. The G1 collector is fully parallel and mostly concurrent, meaning that it does pause applications threads momentarily, but only during certain phases of collection.
How Does G1 Work?
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 [Detlefs04]. It does this while also achieving high throughput, which is an important point when comparing it to other real-time collectors.
The G1 collector divides its work into multiple phases, each described below, which operate on a heap broken down into equally sized regions (see Figure 1). In the strictest sense, the heap doesn't contain generational areas, although a subset of the regions can be treated as such. This provides flexibility in how garbage collection is performed, which is adjusted on-the-fly according to the amount of processor time available to the collector.
Regions are further broken down into 512 byte sections called cards (see Figure 2). Each card has a corresponding one-byte entry in a global card table, which is used to track which cards are modified by mutator threads. Subsets of these cards are tracked, and referred to as Remembered Sets (RS), which is discussed shortly.
The G1 collector works in stages. The main stages consist of remembered set (RS) maintenance, concurrent marking, and evacuation pauses. Let's examine these stages now.
RS Maintenance
Each region maintains an associated subset of cards that have recently been written to, called the Remembered Set (RS). Cards are placed in a region's RS via a write barrier, which is an efficient block of code that all mutator threads must execute when modifying an object reference. To be precise, for a particular region (i.e., region a), only cards that contain pointers from other regions to an object in region a are recorded in region a's RS (see Figure 3). A region's internal references, as well as null references, are ignored.
In reality, each region's remembered set is implemented as a group of collections, with the dirty cards distributed amongst them according to the number of references contained within. Three levels of courseness are maintained: sparse, fine, and course. It's broken up this way so that parallel GC threads can operate on one RS without contention, and can target the regions that will yield the most garbage. However, it's best to think of the RS as one logical set of dirty cards, as the diagrams show.
Concurrent Marking
Concurrent marking identifies live data objects per region, and maintains the pointer to the next free byte, called top. There are, however, small stop-the-world pauses (described further below) that occur to ensure the correct heap state. A marking bitmap is maintained to create a summary view of the live objects within the heap. Each bit in the bitmap corresponds to one word within the heap (an area large enough to contain an object pointer; see Figure 4). A bit in the bitmap is set when the object it represents is determined to be a live object. In reality there are two bitmaps: one for the current collection, and a second for the previously completed collection. This is one way that changes to the heap are tracked over time.
Marking is done in three stages:
- Marking Stage. The heap regions are traversed and live objects are marked:
- First, since this is the beginning of a new collection, the current marking bitmap is copied to the previous marking bitmap, and then the current marking bitmap is cleared.
- Next, all mutator threads are paused while the current TAMS pointer is moved to point to the same byte in the region as the top (next free byte) pointer.
- Next, all objects are traced from their roots, and live objects are marked in the marking bitmap. We now have a snapshot of the heap.
- Next, all mutator threads are resumed.
- Next, a write buffer is inserted for all mutator threads. This barrier records all new object allocations that take place after the snapshot into change buffers.
- Re-marking Stage. When the heap reaches a certain percentage filled, as indicated by the number of allocations since the snapshot in the Marking Stage, the heap is re-marked:
- As buffers of changed objects fill up, the contained objects are marked in the marking bitmap concurrently.
- When all filled buffers have been processed, the mutator threads are paused.
- Next, the remaining (partially filled) buffers are processed, and those objects are marked also.
- Cleanup Stage. When the Re-mark Stage completes, counts of live objects are maintained:
- All live objects are counted and recorded, per region, using the marking bitmap.
- Next, all mutator threads are paused.
- Next, all live-object counts are finalized per region.
- The TAMS pointer for the current collection is copied to the previous TAMS pointer (since the current collection is basically complete).
- The heap regions are sorted for collection priority according to a cost algorithm. As a result, the regions that will yield the highest numbers of reclaimed objects, at the smallest cost in terms of time, will be collected first. This forms what is called a collection set of regions.
- All mutator threads are resumed.
All of this work is done so that objects that are in the collection set are reclaimed as part of the evacuation process. Let's examine this process now.
Evacuation and Collection
This step is what it's all about -- reclaiming dead objects and shaping the heap for efficient object allocation. The collection set of regions (from the Concurrent Marking process defined above) forms a subset of the heap that is used during this process. When evacuation begins, all mutator threads are paused, and live objects are moved from their respective regions and compacted (moved together) into other regions. Although other garbage collectors might perform compaction concurrently with mutator threads, it's far more efficient to pause them. Since this operation is only performed on a portion of the heap -- it compacts only the collection set of regions -- it's a relatively quick, low-pause, operation. Once this phase completes, the GC cycle is complete.
To help limit the total pause time, much of the evacuation is done in parallel with multiple GC threads. The strategy for parallelization involves the following techniques:
- GC TLABS: The use of thread local allocation buffers (TLAB) for the GC threads eliminates memory-related contention amongst the GC threads. Forwarding pointers are inserted in the GC TLABs for evacuated live objects.
- Work Competition: GC threads compete to perform any of a number of GC-related tasks, such as maintaining remembered sets, root object scanning to determine reachability (dead objects are ignored), and evacuating live objects.
- Work Stealing: Part of mathematical systems theory, the work done by the GC threads is unsynchronized and executed arbitrarily by all of the threads simultaneously. This chaos-based algorithm equates to a group of threads that race to complete the list of GC-related tasks as quickly as they can without regard to one another. The end result, despite the apparent chaos, is a properly collected group of heap regions.
Note: The CMS and parallel collectors, described earlier, also use work competition and work stealing techniques to achieve greater efficiency.
Conclusion
The G1 collector is still considered experimental, but can be enabled in Java SE 6 Update 14 with the following two command-line parameters:
1
2
|
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC |
Much of the G1 processing and behavior can be controlled by explicitly setting optional command-line parameters. See the sidebar entitled "Tuning the G1 Collector" to tune G1 behavior.
Tuning the G1 Collector
Let's review some command-line parameters that enable you to tune the behavior of G1. For instance, to suggest a GC pause time goal, use the following parameter:
With G1, a time interval can be specified during which a GC pause applies. In other words, no more than 50 milliseconds out of every second:
Of course, these are only targets and there are no guarantees they will be met in all situations. However, G1 will attempt to meet these targets where it can.
Alternatively, the size of the young generation can be specified explicitly to alter evacuation pause times:
To run G1 at its full potential, add the following two parameters:
However, Sun warns that as of this version of G1, the use of these parameters may produce in a race condition and result in an error. However, it's worth a try to see if your application works safely with them set. If so, you'll benefit from the best GC performance that G1 can offer.
—EJB |
In terms of GC pause times, Sun states that G1 is sometimes better and sometimes worse than CMS. As G1 is still under development, the goal is to make G1 perform better than CMS and eventually replace it in a future version of Java SE (the current target is Java SE 7). While the G1 collector is successful at limiting total pause time, it's still only a soft real-time collector. In other words, it cannot guarantee that it will not impact the application threads' ability to meet its deadlines, all of the time. However, it can operate within a well-defined set of bounds that make it ideal for soft real-time systems that need to maintain high-throughput performance.
If your application requires guaranteed real-time behavior even with garbage collection, your only choice is a real-time garbage collector such as those that come with Sun's Java RTS or IBM's WebSphere RT products. However, if low pause times and soft real-time behavior is your goal, the G1 collector should suit it well.
References
[Bruno09] Bruno, Eric, and Bollella, Greg, Real-Time Java Development with Java RTS, Pearson Publishing, 2009
[Detlefs04] Detlefs, et. al., Garbage-First Garbage Collection, Sun Microsystems Research Laboratories, 2004.
[McCarthy58] McCarthy, John, LISP: A Programming System for Symbolic Manipulations, Communications of the ACM, 1958.
相关推荐
精品资料(2021-2022年收藏的)美国加州教材G1简介及开班信息.doc
尽管G1现在的配置已经较为陈旧,但对于想要体验Android发展早期的经典之作的用户而言,仍有一定的收藏价值。 #### G2: Magic 作为G系手机的第二代产品,HTC Magic(G2)延续了HTC在智能手机领域的创新精神。 - **...
- 密度表达式为 ρ石=G1ρ水/(G1-G2)。 6. **刻度尺测石块密度**(杠杆法): - 结合杠杆平衡条件,当一个石块浸入水中时,杠杆两端的力臂比例可推算出密度。 - 密度表达式为 ρA=ρ水 b/(b-c),其中ρA表示石块...
\\system\\etc\\favorites.xml 收藏夹 \\system\\etc\\firmware 固件信息 \\system\\etc\\gps.conf GPS设置文件 \\system\\etc\\hcid.conf内核HCID配置文件 \\system\\etc\\hosts 网络DNS缓存 \\system\\etc\\init....
使用ST72系列单片机,如ST72264G1,这是一种由ST微电子公司推出的8位单片机,具有出色的抗干扰能力和低功耗特性。它的主要特点包括: - 宽电源电压范围(2.4V至5.5V),内置数字看门狗。 - 丰富的I/O功能,多个...
8. **JTG-ZW-G1型点型紫外火焰探测器**:专门探测紫外线辐射,快速响应火焰。 以上只是部分设备的技术特点和参数,实际应用中应根据具体环境和需求选择合适的设备,并确保所有设备都已通过相关检验报告,以确保系统...
Android 的首个商用设备T-Mobile G1于2008年9月22日发布,这是一部由HTC制造的手机,支持WCDMA/HSPA网络和Wi-Fi。Android 不仅仅是一个操作系统,它还包括了一个完整的软件生态,涵盖操作系统核心、用户界面以及各种...
在本文档中,我们关注的是RIM公司推出的Blackberry Bold智能手机如何在中国市场上与Apple的iPhone和Google的G1等竞品竞争。 一、市场环境分析 中国的智能手机市场竞争激烈,各大品牌市场份额争夺战愈演愈烈。据...
【巴塞尔协议】巴塞尔协议在风险管理方面规定,银行应计算不同产品线的风险权重,如题中G1-8乘以β1-8的和,来衡量整体风险暴露。 【破产与贷款诈骗】破产是指债务人无法偿还到期债务且资产不足以清偿全部债务的...
1. 细胞增殖周期性:细胞增殖是指细胞通过分裂产生新的细胞,这个过程具有周期性,即细胞从一次分裂完成到下一次分裂完成的全过程,包括G1期(DNA合成前期)、S期(DNA合成期)、G2期(DNA合成后期)和M期(分裂期)...
此外,文档还包含了一个目录,显示了更详细的章节结构,例如“G1部分—钢结构工程的项目规范”,这可能涵盖了材料选择、设计原则、制造标准、现场安装要求、质量控制和检验程序等多个方面。这些章节将深入解释如何...
- 整屏参数:像素点间距4mm,像素点密度62500点/m²,采用1R1G1B的配置,LED封装形式为SMD 2121。屏幕分辨率为2767*192,整屏像素点531264点,驱动方式为恒流驱动16扫,刷新率在800Hz至5000Hz之间,灰度等级从4096到...
3. 细胞周期的理解:细胞周期包括间期(G1、S、G2)和分裂期(M),其中S期是DNA复制的时期,G2期则为准备分裂的阶段,M期包括前期、中期、后期和末期,染色体在中期最易辨认。 4. 有丝分裂图像分析:有丝分裂的...
7. **可变增长模型**:对于股息增长率在不同时间段变化的情况,如二元增长模型,股息在前L期以g1增长,之后以g2增长。计算内在价值更为复杂,需要分别对两个阶段的现金流进行贴现。 在实际应用中,投资者需要结合...
对于两段三次 Bezier 曲线在 P4(Q1) 处达到 G1 连续,需要满足的是曲线的切向量在连接点处相等,即第一段曲线在 P4 的导数等于第二段曲线在 Q1 的导数。 这些知识点是计算机图形学基础课程中的核心内容,涉及图像...
1. 细胞周期通常包括间期(G1、S、G2)和分裂期(M期),其中S期为DNA复制期。题目中提到的a到d表示连续的细胞周期,一个完整的细胞周期是从一个细胞开始分裂到其子细胞再次进入分裂期,即a至c代表一个细胞周期,...
"个人收藏"表明这是一份私人珍藏的学习资料,可能包含了作者在理解和应用JVM方面的独特见解和经验总结。 【标签】"JVM"是Java虚拟机的缩写,它是Java程序运行的基础,负责解释执行字节码并管理内存。"JAVA进阶高级...
- JDK 7继续使用CMS(并发标记扫描)和G1(Garbage First)垃圾收集器,提高了服务器环境下的内存管理效率。 7. **部署和安全性**: - 更新了Java Web Start和Java插件,提升了安全性和用户体验。 8. **JMX改进*...
在数控编程中,如G84和G85代表不同的球面加工循环,G00指令需指定F速度,否则无法正确定位,而G0和G1的区别在于运动方式和速度控制。 6. **切削用量**:切削用量包括进给量、背吃刀量和工件转速,它们直接影响到...
2. **创建对象**:在主菜单上选择Objects > New Objects,然后在新对象对话框中选择Group,命名新对象(例如g1),点击OK,进入Group对话框输入变量和值。 3. **定义变量**:在Group对话框的第一列输入变量名Y...