91+92+93-G1垃圾收集器官方文档透彻解读+原理剖析+系统化说明

G1垃圾收集器官方文档透彻解读

G1官网地址:https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html?source=:ow⭕️p:nav:0916BCButton

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

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)。

2.1.2 Java Programming Language

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

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

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

    平台独立性—Java应用程序被编译成字节码,字节码存储在class文件中并加载到JVM中。由于应用程序在JVM中运行,因此它们可以在许多不同的操作系统和设备上运行。

  • Object-Oriented - Java is an object-oriented language that take many of the features of C and C++ and improves upon them.

    面向对象——Java是一种面向对象的语言,它具有C和C++的许多特性并改进了它们。

  • Automatic Garbage Collection - Java automatically allocates and deallocates memory so programs are not burdened with that task.

    自动垃圾收集-Java自动分配和释放内存,这样程序就不会承担这个任务。

  • 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包含大量可用于执行输入/输出、网络连接和日期操作等任务的预制对象。

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.

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

​ 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).

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

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

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

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

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

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

​ 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. (1) The Java Virtual Machine

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

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.

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

2.2.2 Key Hotspot Components

The key components of the JVM that relate to performance are highlighted in the following 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进行调优。

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应用程序时,重点是两个主要目标之一:响应性或吞吐量。随着教程的进行,我们将回顾这些概念

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.

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.

    对于注重吞吐量的应用程序,高暂停时间是可以接受的。由于高吞吐量应用程序将重点放在较长时间内的基准测试上,因此不考虑快速响应时间。

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:

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

  • Can operate concurrently with applications threads like the CMS collector.

    可以与CMS收集器之类的应用程序线程同时操作。

  • Compact free space without lengthy GC induced pause times.

    紧凑的自由空间,没有长的GC诱导的暂停时间。

  • Need more predictable GC pause durations.

    需要更可预测的GC暂停时间。

  • Do not want to sacrifice a lot of throughput performance.

    不想牺牲很多吞吐量性能。

  • Do not require a much larger Java heap.

    不需要更大的Java堆。

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

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.

​ 旧的垃圾收集器(串行、并行、CMS)都将堆结构为三个部分:年轻一代、旧一代和固定内存大小的永久一代。
在这里插入图片描述
​ All memory objects end up in one of these three sections.

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

​ The G1 collector takes a different approach.
在这里插入图片描述
​ 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.

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

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

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

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

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

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

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

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.

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

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.

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

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

RSets 将对象引用跟踪到给定区域。堆中每个区域有一个RSet。RSet支持区域的并行和独立集合。资源集的总体足迹影响小于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.

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

3.3 Recommended Use Cases for G1(推荐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.

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

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

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

  • Full GC durations are too long or too frequent.

    完整的GC持续时间太长或太频繁。

  • The rate of object allocation rate or promotion varies significantly.

    对象分配率或晋升率比较大。

  • Undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second)

    垃圾收集或压缩暂停时间过长(超过0.5到1秒)

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.

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

4. Reviewing GC with the CMS 回顾CMS

4.1 Reviewing Generational GC and CMS 回顾世代GC和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.

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

Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.

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

4.2 CMS Collection Phases

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

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

4.2Reviewing Garbage Collection Steps

​ Next, let’s review CMS Collector operations step by step.

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.

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

4.2.2 How Young GC works in CMS 年轻代中的GC如何在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.

​ 年轻代是浅绿色的,老年代是蓝色的。如果您的应用程序已经运行了一段时间,CMS可能就是这样的。老年代地区到处都是物体。
在这里插入图片描述
​ 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.

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

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 空间。任何已达到老化阈值的旧对象都将升级为老年代。
在这里插入图片描述

4.2.4 After Young GC

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

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

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.
​ 两个stop the world 事件发生:最初的标记和remark。当老年代达到一定的占有率时,CMS就启动了。
在这里插入图片描述
​ (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.

​ (1) 初始标记是一个短暂的暂停阶段,其中存活(可到达)对象被标记。(2) 并发标记在应用程序继续执行时查找存活对象。最后,在(3)标记阶段,重新寻找在(2)前一阶段的并发标记中未标记的对象。

4.2.6 Old Generation Collection - Concurrent Sweep

​ 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

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

​ 在(4)清除阶段之后,您可以看到许多内存已被释放。您还将注意到没有进行压缩。
在这里插入图片描述
​ Finally, the CMS collector will move through the (5) resetting phase and wait for the next time the GC threshold is reached.

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

5. The G1 Garbage Collector Step by Step

5.1 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系统。

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.

​ 实际上,这些区域被映射到了 Eden, Survivor, and old空间的逻辑空间中。
在这里插入图片描述
​ 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, survivor, and old 区域。此外,还有第四种类型的物体称为巨区。这些区域设计用于容纳大小为标准区域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.

​ 堆被分成大约2000个区域。最小尺寸为1MB,最大尺寸为32 MB。蓝色区域包含老年代对象,绿色区域包含新生代对象。
在这里插入图片描述
Note that the regions are not required to be contiguous like the older garbage collectors.

注意,这些区域不需要像旧的垃圾收集器那样是连续的。

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.

​ 存活的对象被转移(即复制或移动)到一个或多个survivor区域。如果满足老化阈值,则某些对象将晋升到老年代区域。
在这里插入图片描述
​ 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.

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

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

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

5: End of a Young GC with G1

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

​ 存活的对象被移动到survivor区域或老年代区域。
在这里插入图片描述

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

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

In summary, the following can be said about the young generation in G1:

综上所述,对于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.

    堆是一个分成多个区域的内存空间。

    新生代的内存是由一组不连续的区域组成的。这使得在需要时调整大小变得容易。

    新生代的垃圾收集,或新生代的GCs,是stop the world的事件。所有应用程序线程都将为该操作停止。

    新生代的GC使用多个线程并行完成。

    存活对象被复制到新的survivor 或 old 区域。

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.这是一个stop the world 事件。对于G1,它是由一个正常的年轻代GC携带的。标记survivor区域(根区域),这些区域可以引用老年代中的对象。
(2) Root Region Scanning根分区扫描Scan 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.扫描survivor区域,寻找到老年代的参考。当应用程序继续运行时会发生这种情况。必须先完成此阶段,然后才能发生新的GC。
(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.完成堆中存活对象的标记。使用一种称为“开始时快照”(SATB)的算法,该算法比CMS收集器中使用的算法快得多。
(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) 对存活对象和完全自由区域执行标记。(Stop the world)清除RSets。(Stop the world)重置空区域并将其返回到空闲列表。(并发)
(*) 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)]. 这些是“stop the world”以将活动对象转移或复制到新的未使用区域。这可以通过记录为“[GC pause(young)”的老年代区域来完成。或者记录为“[GC Pause(mixed)””的新生代和老年代区域。
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收集器中的老年代交互。

6. Initial Marking Phase

​ 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

​ 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

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

​ 将移除并回收空区域。现在计算所有区域的区域存活性。
在这里插入图片描述

9. Copying/Cleanup Phase

​ 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

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

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

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

      当应用程序运行时,同时计算存活信息。

      该存活信息确定了在暂停期间最应该被回收的区域。

      没有像CMS那样的清除阶段。

  • Remark Phase:

    • Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.

      使用快照开始(SATB)算法,比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.

      老年代区域是基于他们的存活性选择的。

6. Command Line Options and Best Practices

6.1 Command Line Options and Best Practices

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

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

6.1.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).

6.1.3 Best Practices

There are a few best practices you should follow when using 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.

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.

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.

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.

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.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值