likes
comments
collection
share

JVM-G1垃圾收集器官方文档翻译

作者站长头像
站长
· 阅读数 5

JVM-G1垃圾收集器官方文档翻译

直接从G1官网翻译,补充个人博客JVM 关于G1内容。

G1官网地址:www.oracle.com/webfolder/t…

看G1可以直接从 第三节,或者第四节开始看就行了。

Getting Started with the G1 Garbage Collector

1. Overview

1.1 Purpose

This tutorial covers the basics of how to use the G1 garbage collector and how it can be used with the Hotspot JVM. You will learn how the G1 collector functions internally, the key command line switches for using G1, and options for logging its operation.


本教程将介绍如何使用G1垃圾收集器以及如何将其与Hotspot JVM一起使用。您将了解G1收集器如何在内部运行,使用G1的关键命令行开关,以及记录其操作的选项。

1.2 Time to Complete

Approximately 1 hour

1.3 Introduction

This OBE covers the basics of Java Virtual Machine(JVM) G1 Garbage Collection (GC) in Java. In the first part of the OBE, an overview of the JVM is provided along with an introduction to Garbage Collection and performance. Next students are provided with a review of how the CMS collector works with the Hotspot JVM. Next, a step by step guide to how Garbage Collection works when using the G1 garbage collection with a Hotspot JVM. Following that, a section is provided covering the Garbage Collection command line options available with the G1 garbage collector. Finally, you will learn about logging options to use with the G1 collector.


这个OBE涵盖了Java中Java虚拟机(JVM)G1垃圾收集(GC)的基础知识。

在OBE的第一部分中,概述了JVM并介绍了垃圾收集和性能;先让学生将回顾CMS收集器如何与Hotspot JVM一起工作;然后,在一步一步介绍如何在Hotspot JVM中使用G1垃圾收集器;接下来将介绍G1垃圾收集器可用的垃圾收集命令行选项;最后,您将了解与G1收集器一起使用的日志记录选项。

1.4 Hardware(硬件) and Software Requirements

The following is a list of hardware and software requirements:

  • A PC running Windows XP or later, Mac OS X or Linux. Note that the hands on is done with Windows 7 and has not been tested on all platforms. However, everything should work fine on OS X or Linux. Also a machine with more than one core is preferable.
  • Java 7 Update 9 or later
  • The latest Java 7 Demos and Samples Zip file

以下是硬件和软件要求列表:

  • 运行在Windows XP或更高版本、Mac OS X或Linux的PC。注意,操作是在Windows 7上完成的,尚未在所有平台上进行测试。然而,在OS X或Linux上,一切都应该正常工作。此外,具有多个核PC是优选的。
  • Java 7 Update 9或更高版本。
  • 最新的Java 7演示和示例Zip文件。

1.5 Prerequisites(预备知识 )

Before starting this tutorial, you should:

  • If you have not done so, download and install the latest version of the Java JDK (JDK 7 u9 or later). Java 7 JDK Downloads
  • Download and install the Demos and Samples zip file from the same location. Unzip the file and place the contents in a directory. For example: C:\javademos

在开始本教程之前,您应该:

  • 如果尚未安装,请下载并安装最新版本的Java JDK(JDK 7 u9或更高版本)。Java 7 JDK下载
  • 从同一位置下载并安装Demos和Samples zip文件。解压缩文件并将内容放在目录中。例如:C:\javademos

Prerequisites 英 [ˌpriːˈrɛkwɪzɪts] 美 [priˈrɛkwəzəts] :预备知识,先决条件,前置条件

2. Java Technology and the JVM

2.1 Java Overview

Java is a programming language and computing platform first released by Sun Microsystems in 1995. It is the underlying technology that powers Java programs including utilities, games, and business applications. Java runs on more than 850 million personal computers worldwide, and on billions of devices worldwide, including mobile and TV devices. Java is composed of a number of key components that, as a whole, create the Java platform.


Java是Sun Microsystems于1995年首次发布的一种编程语言和计算平台。它是支持Java程序的底层技术,包括实用程序、游戏和业务应用程序。Java在全球超过8.5亿台个人电脑上运行,在全球数十亿台设备上运行,包括移动和电视设备。Java由许多关键组件组成,这些组件作为一个整体创建了Java平台。

2.1.1 Java Runtime Edition

When you download Java, you get the Java Runtime Environment (JRE). The JRE consists of the Java Virtual Machine (JVM), Java platform core classes, and supporting Java platform libraries. All three are required to run Java applications on your computer. With Java 7, Java applications run as desktop applications from the operating system, as a desktop application but installed from the Web using Java Web Start, or as a Web Embedded application in a browser (using JavaFX).


下载Java时,您将获得Java运行时环境(JRE)。JRE由Java虚拟机(JVM)、Java平台核心类和支持Java平台库组成。所有这三个都需要在您的计算机上运行Java应用程序。在Java7中,Java应用程序作为桌面应用程序从操作系统运行,作为桌面应用程序但使用Java Web Start从Web安装,或者作为浏览器中的Web嵌入式应用程序(使用JavaFX)。

Edition 英 [ɪˈdɪʃn] 美 [ɪˈdɪʃn] :版本

2.1.2 Java Programming Language

Java is an object-oriented programming language that includes the following features.

  • Platform Independence - Java applications are compiled into bytecode which is stored in class files and loaded in a JVM. Since applications run in a JVM, they can be run on many different operating systems and devices.
  • Object-Oriented - Java is an object-oriented language that take many of the features of C and C++ and improves upon them.
  • Automatic Garbage Collection - Java automatically allocates and deallocates memory so programs are not burdened with that task.
  • Rich Standard Library - Java includes a vast number of premade objects that can be used to perform such tasks as input/output, networking, and date manipulation.

Java是一种面向对象的编程语言,它包括以下特性:

  • 平台独立性——Java应用程序被编译成字节码,字节码存储在class文件中并加载到JVM中。由于应用程序在JVM中运行,因此它们可以在许多不同的操作系统和设备上运行。
  • 面向对象——Java是一种面向对象的语言,它具有C和C++的许多特性并改进了它们。
  • 自动垃圾收集——Java自动分配和释放内存,这样程序就不会承担这个任务。
  • 丰富的标准库——Java包含大量可用于执行输入/输出、网络连接和日期操作等任务的预制对象。
2.1.3 Java Development Kit

The Java Development Kit (JDK) is a collection of tools for developing Java applications. With the JDK, you can compile programs written in the Java Programming language and run them in a JVM. In addition, the JDK provides tools for packaging and distributing your applications.

The JDK and the JRE share the Java Application Programming Interfaces (Java API). The Java API is a collection of prepackaged libraries developers use to create Java applications. The Java API makes development easier by providing the tools to complete many common programming tasks including string manipulation, date/time processing, networking, and implementing data structures (e.g., lists, maps, stacks, and queues).


Java Development Kit(JDK)是一组用于开发Java应用程序的工具。使用JDK,您可以编译用Java编程语言编写的程序并在JVM中运行它们。此外,JDK还提供了打包和分发应用程序的工具。

JDK和JRE共享Java应用程序编程接口(Java API),Java API是开发人员用于创建Java应用程序的预打包库的集合。Java API通过提供工具来完成许多常见的编程任务,包括:字符串操作、日期/时间处理、联网和实现数据结构(例如:列表、映射、堆栈和队列),使开发更加容易。

2.1.4 Java Virtual Machine

The Java Virtual Machine (JVM) is an abstract computing machine. The JVM is a program that looks like a machine to the programs written to execute in it. This way, Java programs are written to the same set of interfaces and libraries. Each JVM implementation for a specific operating system, translates the Java programming instructions into instructions and commands that run on the local operating system. This way, Java programs achieve platform independence.

The first prototype implementation of the Java virtual machine, done at Sun Microsystems, Inc., emulated the Java virtual machine instruction set in software hosted by a handheld device that resembled a contemporary Personal Digital Assistant (PDA). Oracle's current implementations emulate the Java virtual machine on mobile, desktop and server devices, but the Java virtual machine does not assume any particular implementation technology, host hardware, or host operating system. It is not inherently interpreted, but can just as well be implemented by compiling its instruction set to that of a silicon CPU. It may also be implemented in microcode or directly in silicon.

The Java virtual machine knows nothing of the Java programming language, only of a particular binary format, the class file format. A class file contains Java virtual machine instructions (or bytecodes) and a symbol table, as well as other ancillary information.

For the sake of security, the Java virtual machine imposes strong syntactic and structural constraints on the code in a class file. However, any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java virtual machine. Attracted by a generally available, machine-independent platform, implementors of other languages can turn to the Java virtual machine as a delivery vehicle for their languages.


Java虚拟机(JVM)是一种抽象的计算机器。JVM是一个程序,对于要在其中执行的程序来说,它就像一台机器。这样,Java程序就被写入到同一组接口和库中。针对特定操作系统的每个JVM实现,都将Java编程指令转换为在本地操作系统上运行的指令和命令。这样,Java程序就实现了平台无关性。

Java虚拟机的第一个原型实现是在Sun Microsystems,Inc.完成的,它模拟了Java虚拟机指令集,该指令集由类似于当代个人数字助理(PDA)的手持设备托管。Oracle当前的实现在移动、桌面和服务器设备上模拟Java虚拟机,但Java虚拟机不采用任何特定的实现技术、主机硬件或主机操作系统;这不是固有的解释,但也可以通过将其指令集编译为微型CPU的指令集来实现;它也可以用微码或直接用微信处理器来实现。

Java虚拟机对Java编程语言一无所知,只知道特定的二进制格式,即:类文件格式。类文件包含Java虚拟机指令(或字节码)和符号表以及其他辅助信息。

为了安全起见,Java虚拟机对类文件中的代码施加了很强的语法和结构约束。但是,任何具有可用有效类文件表示的功能的语言都可以由Java虚拟机托管。其他语言的实现者被一个普遍可用的、与机器无关的平台所吸引,可以将Java虚拟机作为其语言的传递工具。

instruction 英 [ɪnˈstrʌkʃn] 美 [ɪnˈstrʌkʃn] : n. 说明; (计算机的)指令

emulated 英 [ˈemjuleɪtɪd] 美 [ˈemjuleɪtɪd] : v. 努力赶上,同…竞争;仿真,模仿

handheld 英 [hændˈheld] 美 [hændˈheld] : adj. 手持式的;便携式的

resembled 英 [rɪˈzembld] 美 [rɪˈzembld] : v. 看起来像;显得像

contemporary 英 [kənˈtemprəri] 美 [kənˈtempəreri] : adj. 属同时期的;现代的 ;n. 同代人;同龄人

assume 英 [əˈsjuːm] 美 [əˈsuːm] :v. 假定,认为

ancillary 英 [ænˈsɪləri] 美 [ˈænsəleri] : v. 辅助的,附属的;n. 助手,随从

imposes 英 [ɪmˈpəʊzɪz] 美 [ɪmˈpoʊzɪz] :v. 推行,采用;强制实行,迫使

be hosted by 由....托管

syntactic 英 [sɪnˈtæktɪk] 美 [sɪnˈtæktɪk] :adj. 句法的

constraints 英 [kənˈstreɪnts] 美 [kənˈstreɪnts] :n. 限制,约束

attracted 英 [əˈtræktɪd] 美 [əˈtræktɪd] : v. 吸引,使喜爱;引起(反应)

2.2. Exploring the JVM Architecture

2.2.1 Hotspot Architecture

The HotSpot JVM possesses an architecture that supports a strong foundation of features and capabilities and supports the ability to realize high performance and massive scalability. For example, the HotSpot JVM JIT compilers generate dynamic optimizations. In other words, they make optimization decisions while the Java application is running and generate high-performing native machine instructions targeted for the underlying system architecture. In addition, through the maturing evolution and continuous engineering of its runtime environment and multithreaded garbage collector, the HotSpot JVM yields high scalability on even the largest available computer systems.

The main components of the JVM include the class loader, the runtime data areas, and the execution engine.


HotSpot JVM拥有一个支持特征和能力的强大基础架构,并支持实现高性能和大规模可扩展性的能力。例如,HotSpot JVM JIT编译器生成动态优化。换句话说,它们在Java应用程序运行时做出优化决策,并生成针对底层系统架构的高性能本机指令。 此外,HotSpot JVM通过其运行时环境和多线程垃圾收集器的成熟演进和持续工程,在即使是最大的可用计算机系统上也能获得高的可伸缩性。

JVM的主要组件包括类加载器、运行时数据区(包括:方法去,堆,虚拟机栈(Java Threads),程序计数器,本地方法栈(Native Internal Threads))和执行引擎;如下图所示: JVM-G1垃圾收集器官方文档翻译

architecture 英 [ˈɑːkɪtektʃə(r)] 美 [ˈɑːrkɪtektʃər] : n. 建筑学;体系结构

foundation 英 [faʊnˈdeɪʃn] 美 [faʊnˈdeɪʃn] : n. 地基;房基;基础;基本原理;基金会;(机构或组织的)创建,创办

optimizations 英 [ˌɒptɪmaɪˈzeɪʃ(ə)nz] 美 [ɑptəməˈzeɪʃənz] : n. 优化;最佳化

underlying 英 [ˌʌndəˈlaɪɪŋ] 美 [ˌʌndərˈlaɪɪŋ] : adj. 根本的,潜在的;下层的;v. 构成…的基础;作为…的原因

scalability 英 [skeɪləˈbɪlɪti] 美 [skeɪləˈbɪlɪti] :n. 可扩展性;可伸缩性

maturing 英 [məˈtʃʊəɪŋ] 美 [məˈtʃʊrɪŋ] :成熟

evolution 英 [ˌiːvəˈluːʃn] 美 [ˌiːvəˈluːʃn] :n. 进化;演变;发展;渐进

continuous 英 [kənˈtɪnjuəs] 美 [kənˈtɪnjuəs] :adj. 不断的;持续的

multithreaded 英 [ˌmʌltɪˈθrɛdɪd] 美 [ˌmʌltiˈθrɛdəd] :adj. 多线程的

2.2.2 Key Hotspot Components

JVM-G1垃圾收集器官方文档翻译

The key components of the JVM that relate to performance are highlighted in the up image.

There are three components of the JVM that are focused on when tuning performance. The heap is where your object data is stored. This area is then managed by the garbage collector selected at startup. Most tuning options relate to sizing the heap and choosing the most appropriate garbage collector for your situation. The JIT compiler also has a big impact on performance but rarely requires tuning with the newer versions of the JVM.


JVM中与性能相关的关键组件在上图中突出显示。 (紫色阴影的部分:堆,JIT编译器,垃圾收集器)

在调整性能时,JVM有三个组件是重点关注的。 ①堆是存储对象数据的地方。然后,该区域由启动时选择的垃圾收集器管理。②大多数优化选项都与调整堆大小和根据您的情况选择最合适的垃圾收集器有关。③JIT编译器对性能也有很大的影响,但很少需要使用较新版本的JVM进行调优。

2.3 Performance Basics

Typically, when tuning a Java application, the focus is on one of two main goals: responsiveness or throughput. We will refer back to these concepts as the tutorial progresses.

通常,在调整Java应用程序时,重点是两个主要目标之一:响应性或吞吐量。随着教程的进行,我们将回顾这些概念。

tutorial 英 [tjuːˈtɔːriəl] 美 [tuːˈtɔːriəl] :adj. 辅导的;导师的;私人教师的;n.教程;辅导材料;使用说明书

progresses 英 [prəˈɡresɪz] 美 [prəˈɡresɪz] : n. 进步,进展; v. 进步;改进

2.3.1 Responsiveness(响应性)

Responsiveness refers to how quickly an application or system responds with a requested piece of data. Examples include:

  • How quickly a desktop UI responds to an event
  • How fast a website returns a page
  • How fast a database query is returned
  • For applications that focus on responsiveness, large pause times are not acceptable. The focus is on responding in short periods of time.

响应性是指应用程序或系统对请求的数据的响应速度。示例包括:

  • 桌面UI对事件的响应速度
  • 网站返回页面的速度
  • 数据库查询的返回速度
  • 对于注重响应性的应用程序,大的暂停时间是不可接受的。重点是在短时间内做出反应。
2.3.2 Throughput(吞吐量)

Throughput focuses on maximizing the amount of work by an application in a specific period of time. Examples of how throughput might be measured include:

  • The number of transactions completed in a given time.
  • The number of jobs that a batch program can complete in an hour
  • The number of database queries that can be completed in an hour.
  • High pause times are acceptable for applications that focus on throughput. Since high throughput applications focus on benchmarks over longer periods of time, quick response time is not a consideration.

吞吐量主要是指在特定的时间内使应用程序的工作量最大化。如何测量吞吐量的示例包括:

  • 在给定时间内完成的事务数
  • 批处理程序在一小时内可以完成的作业数
  • 一小时内可以完成的数据库查询数
  • 对于注重吞吐量的应用程序,可以接受高暂停时间。由于高吞吐量应用程序专注于较长时间内的基准测试,因此不需要考虑快速响应时间

maximizing 英 [ˈmæksɪmaɪzɪŋ] 美 [ˈmæksɪmaɪzɪŋ] :v. 最大化

benchmarks 英 [ˈbentʃmɑːks] 美 [ˈbentʃmɑːrks] :n. 基准

3. The G1 Garbage 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.

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.


垃圾优先(G1)收集器是服务器风格的垃圾收集器,目标是具有大内存的多处理器计算机。它以很高的概率满足垃圾收集(GC)暂停时间目标,同时实现高吞吐量。在 Oracle JDK 7 Update 4和更高版本中完全支持G1垃圾收集器。G1收集器是为以下应用而设计的:

  • 可以与CMS收集器之类的应用程序线程同时操作。
  • 紧凑的自由空间,没有长的GC诱导的暂停时间。
  • 需要更可预测的GC暂停时间。
  • 不想牺牲很多吞吐量性能。
  • 不需要更大的Java堆。

G1计划作为并发标记扫描收集器(CMS)的长期替代品。与CMS相比,G1是一个更好的解决方案。一个区别是G1是一个压缩收集器。G1足够紧凑,完全避免使用细粒度的自由列表进行分配,而是依赖于区域(region)。这大大简化了收集器的部分工作,并且基本上消除了潜在的碎片问题。此外,G1提供了比CMS收集器更可预测的垃圾收集暂停,并允许用户指定所需的暂停目标。

meet 英 [miːt] 美 [miːt] : v.满足;遇见;开会;(与…)会面 ;n.体育比赛,运动会;adj.适合的

compact 英 [kəmˈpækt ] 美 [ˈkɑːmpækt] :n. 契约,协议,合约;小汽车。adj.紧凑的,紧密的。vt.把…紧压在一起(或压实)

duration 英 [djuˈreɪʃn] 美 [duˈreɪʃn] :n. 期间,持续时间

sacrifice 英 [ˈsækrɪfaɪs] 美 [ˈsækrɪfaɪs] n. 牺牲;祭品 v. 牺牲,献出

fine-grained 细粒度的

simplifies 英 [ˈsɪmplɪfaɪz] 美 [ˈsɪmplɪfaɪz] v.使简化,使简易

considerably 英 [kənˈsɪdərəbli] 美 [kənˈsɪdərəbli] :adv. 非常,相当多地

eliminate 英 [ɪˈlɪmɪneɪt] 美 [ɪˈlɪmɪneɪt] :vt. 排除;清除

fragmentation 英 [ˌfræɡmenˈteɪʃn] 美 [ˌfræɡmenˈteɪʃn] :n. 碎裂(化),片段,摘录

3.1 G1 Operational Overview

The older garbage collectors (serial, parallel, CMS) all structure the heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.

All memory objects end up in one of these three sections.

旧的垃圾收集器(Serial、Parallel、CMS)都将堆结构为三个部分:新生代,老年代和固定内存大小的永久代。

所有的内存对象都会放在这三个部分中的一个部分。

JVM-G1垃圾收集器官方文档翻译

The G1 collector takes a different approach. G1收集器才用区别于上面所有的垃圾收集器的堆内存结构。

JVM-G1垃圾收集器官方文档翻译

The heap is partitioned into a set of equal-sized heap regions, each a contiguous range of virtual memory. Certain region sets are assigned the same roles (eden, survivor, old) as in the older collectors, but there is not a fixed size for them. This provides greater flexibility in memory usage.

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.


堆被划分为一组大小相等的堆区域,每个区域都是虚拟内存的连续区域。某些区域集被分配了与旧收集器中相同的角色(eden、survivor、old),但它们没有固定的大小。这为内存使用提供了更大的灵活性。

在执行垃圾收集时,G1的操作方式与CMS收集器类似。G1执行一个并发的全局标记阶段,以确定整个堆中对象的存活。标记阶段完成后,G1知道哪些区域大部分是空的。它首先聚集在这些区域,这通常会产生大量的自由(可用)空间。这就是为什么这种垃圾收集方法称为“垃圾优先”。顾名思义,G1将其收集和压缩活动集中在堆中可能充满可回收对象(即垃圾)的区域。G1使用暂停预测模型来满足用户定义的暂停时间目标,并基于指定的暂停时间目标选择要收集的区域数。

被G1确定为可回收的区域是通过离散收集的垃圾。G1将对象从堆的一个或多个区域复制到堆的单个区域,在这个过程中压缩和释放内存。这种离散在多个处理器上并行执行,以减少暂停时间并提高吞吐量。因此,对于每个垃圾收集,G1在用户定义的暂停时间内连续工作以减少碎片。这超出了前面两种方法的能力。CMS(并发标记扫描)垃圾收集器不进行压缩。Parallel Old垃圾收集器执行整个堆压缩,这会导致相当长的暂停时间。

需要注意的是,G1不是实时收集器。它满足设定的暂停时间目标的概率很高,但不是绝对确定的。根据以前收集到的数据,G1估计在用户指定的目标时间内可以收集多少个区域。因此,收集器对收集区域的成本有一个相当精确的模型,并且它使用该模型来确定在停留在暂停时间目标内要收集哪些和多少区域。

注意:G1既有并发(与应用程序线程一起运行,例如:优化、标记、清理)阶段,也有并行(多线程,例如:Stop the World)阶段。完整的垃圾收集仍然是单线程的,但是如果调整得当,应用程序应该避免full gc。

end up 最终,结束,死亡

approach 英 [əˈprəʊtʃ] 美 [əˈproʊtʃ] : n. 方法;v. 靠近;接洽;建议

certain 英 [ˈsɜːtn] 美 [ˈsɜːrtn] :adj. 肯定,确定,确实;某事,某人,某种,某些

manner 英 [ˈmænə(r)] 美 [ˈmænər] :n. 方式,方法,举止,态度,礼貌,礼仪,规矩

reclaimable 可回收的

ripe 英 [raɪp] 美 [raɪp] adj.成熟的

evacuation 英 [ɪˌvækju'eɪʃ(ə)n] 美 [ɪˌvækju'eɪʃ(ə)n] n. 疏散,撤离,转移,迁移,回收

accurate 英 [ˈækjərət] 美 [ˈækjərət] :adj. 精确的,正确无误的,准确的

3.2 G1 Footprint

If you migrate from the Parallel Old GC 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.

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.


如果从Parallel Old GC或CMS收集器迁移到G1,您可能会看到更大的JVM进程尺寸。这在很大程度上与“accounting”数据结构有关,如:记忆集和集合集。

RSets 将对象引用跟踪到给定区域。堆中每个区域有一个RSet。RSet支持区域的并行和独立集合。资源集的总体足迹影响小于5%。

CSets 将在GC中收集的区域集。在GC过程中,CSet中的所有实时数据都被清空(复制/移动)。区域集可以是Eden, survivor, and/or old generation。CSets 对JVM大小的影响小于1%。

migrate 英 [maɪˈɡreɪt] 美 [ˈmaɪɡreɪt] :v. (随季节变化)迁徙,移居,迁移,移动,转移

overall 英 [ˈəʊvərɔːl] 美 [ˈoʊvərɔːl] :adj.全面的,综合的,总体的;adv.全部,总计,大致上,总体上

footprint n.内存占用

3.3 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 Parallel Old GC 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 Parallel Old GC 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的第一个重点是为运行需要大量GC延迟的应用程序的用户提供解决方案。这意味着堆大小约为6GB或更大,并且稳定且可预测的暂停时间低于0.5秒。

如果应用程序具有以下一个或多个特性,那么使用CMS或Parallel Old GC垃圾收集器运行的应用程序将有利于切换到G1。

  • Full GC持续时间太长或太频繁。
  • 对象分配率或晋升率比较大。
  • 垃圾收集或压缩暂停时间过长(超过0.5到1秒)

如果您使用的是CMS或Parallel Old GC,并且您的应用程序没有遇到长时间的垃圾收集暂停,那么可以使用当前收集器。使用最新的JDK不需要更改为G1收集器。

latency 英 ['leɪtənsɪ] :n. 延迟,潜伏

trait 英 [treɪt] 美 [treɪt] : n. 特质,特性,特点

varies 英 [ˈveəriz] 美 [ˈveriz] v. (大小、形状等)相异,不同,有别;(根据情况)变化,改变

significantly 英 [sɪɡˈnɪfɪkəntli] 美 [sɪɡˈnɪfɪkəntli] adv. 有重大意义地;显著地

undesired 英 [ˌʌndɪˈzaɪəd] 美 [ʌndɪˈzaɪərd] :adj. 不想要的

requirement 英 [rɪˈkwaɪəmənt] 美 [rɪˈkwaɪərmənt] :n. 要求

4. Reviewing GC with the CMS

4.1 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.


并发标记清除(CMS)收集器(也称为并发低延迟收集器)收集永久代。它尝试通过与应用程序线程同时执行大多数垃圾收集工作来最小化由于垃圾收集而导致的暂停。 通常,并发低延迟收集器不会复制或压缩存活对象。垃圾收集是在不移动活动对象的情况下完成的。如果碎片化成为一个问题,请分配一个更大的堆。

Note:年轻代的CMS收集器使用与并行收集器相同的算法。

4.2 CMS Collection Phases

The CMS collector performs the following phases on the old generation of the heap:

Next, let's review CMS Collector operations step by step.

CMS收集器对老年代堆执行以下阶段:

接下来,让我们来回顾CMS 收集器的各个阶段

4.2.1 Heap Structure for CMS Collector CMS

The heap is split into three spaces.

Young generation is split into Eden and two survivor spaces. Old generation is one contiguous space. Object collection is done in place. No compaction is done unless there is a full GC.

JVM-G1垃圾收集器官方文档翻译


堆被分成三个空间。

年轻代被分成Eden 、two survivor spaces。老年代是一个连续的空间。对象收集就地完成。除非有Full GC,否则不会进行压缩。

4.2.2 How Young GC works in CMS

The young generation is colored light green and the old generation in blue. This is what the CMS might look like if your application has been running for a while. Objects are scattered around the old generation area.

With CMS, old generation objects are deallocated in place. They are not moved around. The space is not compacted unless there is a full GC.

JVM-G1垃圾收集器官方文档翻译


年轻代是浅绿色的,老年代是蓝色的。如果您的应用程序已经运行了一段时间,CMS可能就是这样的。老年代区域到处都是对象。

使用CMS,老年代对象将就地释放。它们不会被移动。除非有Full GC,否则空间不会被压缩。

deallocate:释放,解除分配

4.2.3 Young Generation Collection

Live objects are copied from the Eden space and survivor space to the other survivor space. Any older objects that have reached their aging threshold are promoted to old generation.

存活的对象从Eden 空间和survivor 空间复制到其他survivor 空间。任何已达到老化阈值的旧对象都将升级为老年代。

JVM-G1垃圾收集器官方文档翻译

4.2.4 After Young GC

JVM-G1垃圾收集器官方文档翻译

After a young GC, the Eden space is cleared and one of the survivor spaces is cleared.

Newly promoted objects are shown in dark blue on the diagram. The green objects are surviving young generation objects that have not yet been promoted to old generation.


在一次年轻代GC之后,Eden 空间被清除,其中一个Survivor 空间被清除。

新晋升的对象在图表上以深蓝色显示。绿色对象是还没有被晋升到老年代的年轻代对象。

4.2.5 Old Generation Collection with CMS

Two stop the world events take place: initial mark and remark. When the old generation reaches a certain occupancy rate, the CMS is kicked off.

(1) Initial mark is a short pause phase where live (reachable) objects are marked.

(2) Concurrent marking finds live objects while the application continues to execute.

Finally, in the (3) remark phase, objects are found that were missed during (2) concurrent marking in the previous phase.

JVM-G1垃圾收集器官方文档翻译

两个stop the world 事件发生:最初的标记和remark。当老年代达到一定的占有率时,CMS就启动了。

(1) 初始标记是一个短暂的暂停阶段,其中存活(可到达)对象被标记。

(2) 并发标记在应用程序继续执行时查找存活对象。

最后,在(3)标记阶段,重新寻找在(2)前一阶段的并发标记中未标记的对象。这个就是因为在并发标记阶段过程中, GC线程和用户线程并发工作,导致有新引用产生,(且注意:CMS是基于增量更新来做并发标记的,G1使用原始快照的方式做并发标记。)

4.2.6 Old Generation Collection - Concurrent Sweep

JVM-G1垃圾收集器官方文档翻译

Objects that were not marked in the previous phase are deallocated in place. There is no compaction.

Note: Unmarked objects == Dead Objects


在前一阶段中未标记的对象将就地释放。没有压缩。

注意:为标记的对象 == 要被回收的对象

4.2.7 Old Generation Collection - After Sweeping

JVM-G1垃圾收集器官方文档翻译

After the (4) Sweeping phase, you can see that a lot of memory has been freed up. You will also notice that no compaction has been done.

Finally, the CMS collector will move through the (5) resetting phase and wait for the next time the GC threshold is reached.


在(4)清除阶段之后,您可以看到许多内存已被释放。您还将注意到没有进行压缩。

最后,CMS收集器将通过(5)重置阶段,并等待下一次达到GC阈值。

5. The G1 Garbage Collector Step by Step

5.1 Young Generation Collection with G1

The G1 collector takes a different approach to allocating the heap. The pictures that follow review the G1 system step by step.

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不等的区域。

JVM-G1垃圾收集器官方文档翻译

2: G1 Heap Allocation

JVM-G1垃圾收集器官方文档翻译

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.

Note: At the time of this writing, collecting humongous objects has not been optimized. Therefore, you should avoid creating objects of this size.


实际上,这些区域被映射到了 Eden, Survivor, and Old 的逻辑空间中。

图片中的颜色显示哪个区域与哪个角色关联。存活对象从一个区域转移到另一个区域(即复制或移动)。区域被设计为并行或不停止所有其他应用程序线程并行收集。

如图所示,区域可以分配到 Eden, survivor, and old 区域。此外,还有第四种类型的对象称为 大区(Humongous regions) 。这些区域设计用于容纳大小为标准区域50%或更大的对象。它们存储为一组相邻区域。最后一种类型的区域是堆中未使用的区域。

注意:在撰写本文时,尚未对收集大对象进行优化。因此,应避免创建此大小的对象。

3: Young Generation in G1

JVM-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

JVM-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.

This is a stop the world (STW) pause. Eden size and survivor size is calculated for the next young GC. Accounting information is kept to help calculate the size. Things like the pause time goal are taken into consideration.

This approach makes it very easy to resize regions, making them bigger or smaller as needed.


存活的对象被转移(即复制或移动)到一个或多个survivor区域。如果满足老化阈值,则某些对象将晋升到老年代区域。

这是一个STW暂停。计算下一个新生代GC的Eden大小和survivor大小。保留记录信息有助于计算规模。像暂停时间目标这样的事情都被考虑进去了。

这种方法可以很容易地调整区域大小,使它们根据需要变大或变小。

5: End of a Young GC with G1

JVM-G1垃圾收集器官方文档翻译

Live objects have been evacuated to survivor regions or to old generation regions.

Recently promoted objects are shown in dark blue. Survivor regions in green.

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.

存活的对象被移动到survivor区域或老年代区域。

新晋升的对象显示为深蓝色。绿色的Survivor区域。

综上所述,对于G1的新生代,可以说:

  • 堆是一个分成多个区域的内存空间。
  • 新生代的内存是由一组不连续的区域组成的。这使得在需要时调整大小变得容易。
  • 新生代的垃圾收集,或新生代的GCs,是stop the world的事件。所有应用程序线程都将为该操作停止。
  • 新生代的GC使用多个线程并行完成。
  • 存活对象被复制到新的survivor 或 old 区域。

contiguous 英 [kənˈtɪɡjuəs] 美 [kənˈtɪɡjuəs] adj. 相接的,相邻的

5.2 Old Generation Collection with G1

Like the CMS collector, the G1 collector is designed to be a low pause collector for old generation objects. The following table describes the G1 collection phases on old generation.

与CMS收集器一样,G1收集器被设计为用于老年代对象的低停顿收集器。下表描述了老年代的G1收集阶段。

5.2.1 G1 Collection Phases - Concurrent Marking Cycle Phases

The G1 collector performs the following phases on the old generation of the heap. Note that some phases are part of a young generation collection.

G1收集器在老年代堆上执行以下阶段。注意,有些阶段也是新生代收集器的一部分。

PhaseDescription
(1) Initial Mark (Stop the World Event)This is a stop the world event. With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation.
(2) Root Region ScanningScan survivor regions for references into the old generation. This happens while the application continues to run. The phase must be completed before a young GC can occur.
(3) Concurrent Marking并发标记Find live objects over the entire heap. This happens while the application is running. This phase can be interrupted by young generation garbage collections.
(4) Remark (Stop the World Event)Completes the marking of live object in the heap. Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than what was used in the CMS collector.
(5) Cleanup (Stop the World Event and Concurrent)Performs accounting on live objects and completely free regions. (Stop the world) Scrubs the Remembered Sets. (Stop the world) Reset the empty regions and return them to the free list. (Concurrent)
(*) Copying (Stop the World Event)These are the stop the world pauses to evacuate or copy live objects to new unused regions. This can be done with young generation regions which are logged as [GC pause (young)]. Or both young and old generation regions which are logged as [GC Pause (mixed)].

PhaseDescription
(1) 初始标记(会STW)这是一个stop the world 事件。对于G1,它是由一个正常的年轻代GC携带的。标记survivor区域(根区域),这些区域可以引用老年代中的对象。
(2) 根分区扫描扫描survivor区域,寻找到老年代的引用。当应用程序继续运行时会发生这种情况。必须先完成此阶段,然后才能发生新的GC。
(3) 并发标记在整个堆中查找存活对象。这是在应用程序运行时发生的(就意思是:GC并发标记线程可以和用户线程一起工作)。这个阶段可以被新生代的垃圾收集所中断。
(4) 重新标记(会STW)完成堆中存活对象的标记。使用一种称为“开始时快照”(SATB)的算法,该算法比CMS收集器中使用的算法快得多。
(5) 清除(会STW和并发)这里会做三件事情: ① 对存活对象和完全自由区域执行标记。(Stop the world) ② 清除RSets。(Stop the world) ③ 重置空区域并将其返回到空闲列表。(并发)
复制(会STW)这里“stop the world”会将存活对象转移或复制到新的未使用区域。这可以通过日志为“[GC pause(young)”的老年代区域来完成。或者日志为“[GC Pause(mixed)””的新生代和老年代区域。

piggybacked 英 [ˈpɪɡibæk] 美 [ˈpɪɡibæk] : 背负的,携带的

5.2.2 G1 Old Generation Collection Step by Step

With the phases defined, let's look at how they interact with the old generation in the G1 collector.

在定义了阶段之后,让我们看看它们如何与G1收集器中的老年代交互。

interact 英 [ˌɪntərˈækt] 美 [ˌɪntərˈækt] vi.互动;相互作用

6. Initial Marking Phase

初始标记阶段,会STW

JVM-G1垃圾收集器官方文档翻译

Initial marking of live object is piggybacked on a young generation garbage collection. In the logs this is noted as GC pause (young)(inital-mark).


存活对象的初始标记依赖于年轻代的垃圾收集 (就是首先标记在老年代中有哪些对象是直接被新生代对象引用的) 。在日志中,这被称为“GC pause(young)(inital mark)”。

7. Concurrent Marking Phase

并发标记阶段,GC线程和用户线程一起工作

JVM-G1垃圾收集器官方文档翻译

If empty regions are found (as denoted by the "X"), they are removed immediately in the Remark phase. Also, "accounting" information that determines liveness is calculated.

如果发现空白区域(如“X”所示),则在标记阶段立即将其删除。此外,还计算确定出存活的“accounting”信息。

8. Remark Phase

重新标记(会STW)

JVM-G1垃圾收集器官方文档翻译

Empty regions are removed and reclaimed. Region liveness is now calculated for all regions.


将移除并回收空区域。现在计算所有区域的区域存活性。(该阶段就是由于并发阶段,处理用户线程所产生的新的引用)

9. Copying/Cleanup Phase

复制、清理阶段

JVM-G1垃圾收集器官方文档翻译

G1 selects the regions with the lowest "liveness", those regions which can be collected the fastest. Then those regions are collected at the same time as a young GC. This is denoted in the logs as `[GC pause (mixed)]`. So both young and old generations are collected at the same time.

G1选择“存活”最低的区域,即可以最快被收集的区域。然后这些区域与年轻的GC同时被收集。这在日志中表示为[GC pause(mixed)]。所以,这种情况下新生代和老年代是同时被收集的。

10. After Copying/Cleanup Phase

在复制、清理阶段之后

JVM-G1垃圾收集器官方文档翻译

The regions selected have been collected and compacted into the dark blue region and the dark green region shown in the diagram.

选定的区域已收集并压缩为图中所示的深蓝色区域和深绿色区域。

5.2.3 Summary of Old Generation GC

In summary, there are a few key points we can make about the G1 garbage collection on the old generation.

  • Concurrent Marking Phase:

    • Liveness information is calculated concurrently while the application is running.
    • This liveness information identifies which regions will be best to reclaim during an evacuation pause.
    • There is no sweeping phase like in CMS.
  • Remark Phase:

    • Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.
    • Completely empty regions are reclaimed.
  • Copying/Cleanup Phase:

    • Young generation and old generation are reclaimed at the same time.
    • Old generation regions are selected based on their liveness.

总结,关于老年代上的G1垃圾收集,我们可以提出一些要点。

  • 并发标记阶段:

    • 当应用程序运行时,同时计算存活信息。
    • 该存活信息确定了在暂停期间最应该被回收的区域。
    • 没有像CMS那样的清除阶段。
  • 重新标记阶段:

    • 使用快照开始(SATB)算法,比CMS使用的速度快得多。
    • 完全空的区域被回收。
  • 复制、清理阶段:

    • 新生代和老年代同时被回收。(其实不用再去区分新生代老年代,最后都是按Region回收的)
    • 老年代区域是基于他们的存活性选择的。

5.3 Other

自己对GC做的一些总结,在书籍中摘抄过来,G1收集器的阶段。

  • 首先是:G1如何解决将Java堆分成多个独立Region后,Region里面存在的跨Region引用对象如何解决?

    使用记忆集避免全堆作为GC Roots扫描,但在G1收集器上记忆集的应用其实要复杂很多,它的每个Region都维护有自己的记忆集,这些记忆集会记录下别的Region指向自己的指针,并标记这些指针分别在哪些卡页的范围之内。G1的记忆集在存储结构的本质上是一种哈希表,Key是别的Region的起始地址,Value是一个集合,里面存储的元素是卡表的索引号。这种“双向”的卡表结构(卡表是“我指向谁”,这种结构还记录了“谁指向我”)比原来的卡表实现起来更复杂,同时由于Region数量比传统收集器的分代数量明显要多得多,因此G1收集器要比其他的传统垃圾收集器有着更高的内存占用负担。根据经验,G1至少要耗费大约相当于Java堆容量10%至20%的额外内存来维持收集器工作。

  • 在并发标记阶段如何保证收集线程与用户线程互不干扰地运行?

    这里首先要解决的是用户线程改变对象引用关系时,必须保证其不能打破原本的对象图结构,导致标记结果出现错误。

    CMS收集器采用增量更新算法实现,而G1收集器则是通过原始快照(SATB)算法来实现的。 此外,垃圾收集对用户线程的影响还体现在回收过程中新创建对象的内存分配上,程序要继续运行就肯定会持续有新对象被创建,G1为每一个Region设计了两个名为TAMS(Top at Mark Start)的指针,把Region中的一部分空间划分出来用于并发回收过程中的新对象分配,并发回收时新分配的对象地址都必须要在这两个指针位置以上。G1收集器默认在这个地址以上的对象是被隐式标记过的,即默认它们是存活的,不纳入回收范围。与CMS中的“Concurrent Mode Failure”失败会导致Full GC类似,如果内存回收的速度赶不上内存分配的速度,G1收集器也要被迫冻结用户线程执行,导致Full GC而产生长时间“Stop The World”。

  • 怎样建立起可靠的停顿预测模型?

    用户通过 -XX:MaxGCPauseMillis 参数指定的停顿时间只意味着垃圾收集发生之前的期望值,但G1收集器要怎么做才能满足用户的期望呢?

    G1收集器的停顿预测模型是以衰减均值(Decaying Average)为理论基础来实现的,在垃圾收集过程中,G1收集器会记录每个Region的回收耗时、每个Region记忆集里的脏卡数量等各个可测量的步骤花费的成本,并分析得出平均值、标准偏差、置信度等统计信息。这里强调的“衰减平均值”是指它会比普通的平均值更容易受到新数据的影响,平均值代表整体平均状态,但衰减平均值更准确地代表“最近的”平均状态。换句话说,Region的统计状态越新越能决定其回收的价值。然后通过这些信息预测现在开始回收的话,由哪些Region组成回收集才可以在不超过期望停顿时间的约束下获得最高的收益。

如果我们不去计算用户线程运行过程中的动作(如使用写屏障维护记忆集的操作),G1收集器的运作过程大致可划分为以下四个步骤:

JVM-G1垃圾收集器官方文档翻译

  • 初始标记(Initial Marking):

    仅仅只是标记一下GC Roots能直接关联到的对象,并且修改TAMS指针的值,让下一阶段用户线程并发运行时,能正确地在可用的Region中分配新对象。这个阶段需要停顿线程,但耗时很短,而且是借用进行Minor GC的时候同步完成的,所以G1收集器在这个阶段实际并没有额外的停顿。

  • 并发标记(Concurrent Marking):

    从GC Root开始对堆中对象进行可达性分析,递归扫描整个堆里的对象图,找出要回收的对象,这阶段耗时较长,但可与用户程序并发执行。当对象图扫描完成以后,还要重新处理SATB记录下的在并发时有引用变动的对象。

  • 最终标记(Final Marking):

    对用户线程做另一个短暂的暂停,用于处理并发阶段结束后仍遗留下来的最后那少量的SATB记录。

  • 筛选回收(Live Data Counting and Evacuation):

    负责更新Region的统计数据,对各个Region的回收价值和成本进行排序,根据用户所期望的停顿时间来制定回收计划,可以自由选择任意多个Region构成回收集,然后把决定回收的那一部分Region的存活对象复制到空的Region中,再清理掉整个旧Region的全部空间。这里的操作涉及存活对象的移动,是必须暂停用户线程,由多条收集器线程并行完成的。

从上述阶段的描述可以看出,G1收集器除了并发标记外,其余阶段也是要完全暂停用户线程的,换言之,它并非纯粹地追求低延迟,官方给它设定的目标是在延迟可控的情况下获得尽可能高的吞吐量,所以才能担当起“全功能收集器”的重任与期望。

6. Command Line Options and Best Practices

In this section let's take a look at the various command line options for G1.

在本节中,让我们看看G1的各种命令行选项。

6.1 Basic Command Line

To enable the G1 Collector use: -XX:+UseG1GC

Here is a sample command line for starting the Java2Demo included in the JDK demos and samples download:

java -Xmx50m -Xms50m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar


要启用G1收集器,请使用:-XX:+UseG1GC

以下是启动JDK演示和示例下载中包含的Java2Demo的示例命令行:

java -Xmx50m -Xms50m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar

6.2 Key Command Line Switches
  • -XX:+UseG1GC

    Tells the JVM to use the G1 Garbage collector.

  • -XX:MaxGCPauseMillis=200

    Sets a target for the maximum GC pause time. This is a soft goal, and the JVM will make its best effort to achieve it. Therefore, the pause time goal will sometimes not be met. The default value is 200 milliseconds.

  • -XX:InitiatingHeapOccupancyPercent=45

    Percentage of the (entire) heap occupancy to start a concurrent GC cycle. It is used by G1 to trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations. A value of 0 denotes 'do constant GC cycles'. The default value is 45 (i.e., 45% full or occupied).


  • -XX:+UseG1GC

    告诉JVM使用G1垃圾收集器。

  • -XX:MaxGCPauseMillis=200

    设置最大GC停顿时间的目标。这是一个软目标,JVM将尽最大努力实现它。因此,停顿时间目标有时无法实现。默认值为200毫秒。

  • -XX:InitiatingHeapOccupancyPercent=45

    启动并发GC循环的(整个)堆占用率百分比。G1使用它来基于整个堆的占用率(而不仅仅是其中一代)触发并发GC循环。值0表示“执行恒定GC循环”。默认值为45(即45%已满或已占用)。

denotes 英 [dɪˈnəʊts] 美 [dɪˈnoʊts] v. 标志,预示,象征,表示

6.3 Best Practices

There are a few best practices you should follow when using G1.

在使用G1时,您应该遵循一些最佳实践。


Do not Set Young Generation Size

Explicitly setting young generation size via -Xmn meddles with the default behavior of the G1 collector.

  • G1 will no longer respect the pause time target for collections. So in essence, setting the young generation size disables the pause time goal.
  • G1 is no longer able to expand and contract the young generation space as needed. Since the size is fixed, no changes can be made to the size.

不设置年轻一代大小

通过 -Xmn 显式设置年轻代的大小会干扰G1收集器的默认行为。

  • G1将不再遵守集合的暂停时间目标。因此,从本质上讲,设定年轻代的大小会使停顿时间目标失效。
  • G1不再能够根据需要扩大和缩小年轻一代的空间。由于大小是固定的,因此不能更改大小。

Response Time Metrics

Instead of using average response time (ART) as a metric to set the XX:MaxGCPauseMillis=<N>, consider setting value that will meet the goal 90% of the time or more. This means 90% of users making a request will not experience a response time higher than the goal. Remember, the pause time is a goal and is not guaranteed to always be met.

响应时间指标

不要使用平均响应时间(ART)作为设置 XX:MaxGCPauseMillis=<N>的指标,而是考虑设置在90%或更长时间内满足目标的值。这意味着90%的用户提出请求的响应时间不会超过目标。记住:停顿时间是一个目标,并不能保证总是能够实现。


What is an Evacuation Failure?

A promotion failure that happens when a JVM runs out of heap regions during the GC for either survivors and promoted objects. The heap can't expand because it is already at max. This is indicated in the GC logs when using -XX:+PrintGCDetails by to-space overflow. This is expensive!

  • GC still has to continue so space has to be freed up.
  • Unsuccessfully copied objects have to be tenured in place.
  • Any updates to RSets of regions in the CSet have to be regenerated.
  • All of these steps are expensive.

什么是迁移失败

其实一起就是 survivors 和要晋升的对象 迁移到 空闲Region的过程,迁移之后才开始GC。

当JVM在GC期间耗尽堆区域时,对于 survivors 和要晋升的对象,会发生晋升失败。堆无法扩展,因为它已达到最大值。使用-XX:+PrintGCDetails时,GC日志中会通过指示这一点,以防止空间溢出。这代价太高了!

  • GC仍然必须继续,因此必须释放空间。
  • 未成功复制的对象必须在原地保留。
  • 必须重新生成对CSet中区域的RSet的任何更新。
  • 所有这些步骤代价都是十分高的。

How to avoid Evacuation Failure

To avoid evacuation failure, consider the following options.

  • Increase heap size

    • Increase the -XX:G1ReservePercent=n, the default is 10.
    • G1 creates a false ceiling by trying to leave the reserve memory free in case more 'to-space' is desired.
  • Start the marking cycle earlier

  • Increase the number of marking threads using the -XX:ConcGCThreads=n option.

如何避免迁移失败

为避免迁移失败,请考虑以下选项。

  • 增加堆大小

    • 增加 -XX:G1ReservePercent=n,默认值为10。
    • G1试图保留内存空闲,以防需要更多的“空间”,从而创建了一个错误的上限。
  • 更早开始标记周期

  • 使用 -XX:ConcGCThreads=n 选项增加标记线程数。


Complete List of G1 GC Switches

This is the complete list of G1 GC switches. Remember to use the best practices outlined above.

Option and Default ValueDescription
-XX:+UseG1GCUse the Garbage First (G1) Collector
-XX:MaxGCPauseMillis=nSets a target for the maximum GC pause time. This is a soft goal, and the JVM will make its best effort to achieve it.
-XX:InitiatingHeapOccupancyPercent=nPercentage of the (entire) heap occupancy to start a concurrent GC cycle. It is used by GCs that trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations (e.g., G1). A value of 0 denotes 'do constant GC cycles'. The default value is 45.
-XX:NewRatio=nRatio of new/old generation sizes. The default value is 2.
-XX:SurvivorRatio=nRatio of eden/survivor space size. The default value is 8.
-XX:MaxTenuringThreshold=nMaximum value for tenuring threshold. The default value is 15.
-XX:ParallelGCThreads=nSets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running.
-XX:ConcGCThreads=nNumber of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running.
-XX:G1ReservePercent=nSets the amount of heap that is reserved as a false ceiling to reduce the possibility of promotion failure. The default value is 10.
-XX:G1HeapRegionSize=nWith G1 the Java heap is subdivided into uniformly sized regions. This sets the size of the individual sub-divisions. The default value of this parameter is determined ergonomically based upon heap size. The minimum value is 1Mb and the maximum value is 32Mb.

G1 GC开关完整列表

这是G1 GC开关的完整列表。请记住使用上述最佳实践。

Option and Default ValueDescription
-XX:+UseG1GC使用G1收集器
-XX:MaxGCPauseMillis=n设置最大GC停顿时间的目标。这是一个软目标,JVM将尽最大努力实现它。
-XX:InitiatingHeapOccupancyPercent=n启动并发GC循环的(整个)堆占用率百分比。它由GC使用,它基于整个堆的占用率触发并发GC循环,而不仅仅是其中一代(例如:G1)。值0表示“执行恒定GC循环”。默认值为45。
-XX:NewRatio=n新生代/老年代数量的比率。默认值为2。
-XX:SurvivorRatio=neden/survivor 空间大小的比率。默认值为8。
-XX:MaxTenuringThreshold=n永久代阈值的最大值。默认值为15。
-XX:ParallelGCThreads=n设置在垃圾收集器的并行阶段使用的线程数。默认值随JVM运行的平台而异。
-XX:ConcGCThreads=n并发垃圾收集器将使用的线程数。默认值随JVM运行的平台而异。
-XX:G1ReservePercent=n设置保留堆数量假的上限,以减少升级失败的可能性。默认值为10。
-XX:G1HeapRegionSize=n使用G1,Java堆被细分为大小一致的区域。这将设置各个子分区的大小。此参数的默认值根据堆大小按工效学确定。最小值为1Mb,最大值为32Mb。

explicitly 英 [ɪksˈplɪsɪtli] 美 [ɪkˈsplɪsətli] :adv. 明确地,显式地

essence 英 [ˈesns] 美 [ˈesns] : n.本质,实质

contract 英 [kənˈtrækt] 美 [ kənˈtrækt] :n. 合同,合约 ;v. (使)收缩,缩小

ceiling 英 [ˈsiːlɪŋ] 美 [ˈsiːlɪŋ] :n. 天花板,顶棚,上限; v. 装天花板;装壁板;装隔板

subdivided 英 [ˈsʌbdɪvaɪdɪd] 美 [ˈsʌbdɪvaɪdɪd] :v. (被)再分割,再分,adj. 细分的

uniformly 英 [ˈjuːnɪfɔːmli] 美 [ˈjunəˌfɔrmli] : adv. 均匀地;一致地

ergonomically 英 [ˌɜːɡə'nɒmɪks] 美 [ˌɜːrɡə'nɑːmɪks] : n. 人类工程学;工效学