接着上一次【G1垃圾收集器角色划分与重要概念详解【纯理论】】继续往下,在前几次中已经对G1的理论进行了一个比较详细的了解了,对于G1垃圾收集器最权威的解读肯定得上官网,当咱们将官网的理解透了,那基本上网上对于G1的说明其实最终都是来自于官网,所以接下来会详细来解读Oracle官网对于G1垃圾收集器的权威说明,其网址为:“https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html”,打开如下:
先来解读一下大纲:
好,接下来开启第一小节的解读“Overview”,由于是概览,所以比较简单,大概了解下既可。
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.
【解读:
“This tutorial covers the basics of how to use the G1 garbage collector and how it can be used with the Hotspot JVM. ”
这个指南涵盖了如何使用G1垃圾收集器的基本知识以及它是如何的应用到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收集器内部功作的机理,主要的用于G1的一些命令行的开关,以及用于日志操作的一些选项。】Time to Complete
Approximately 1 hour
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.
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
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
好,第一部分的一带而过了,了解既可,接下来看第二节“Java Technology and the JVM”,如下:
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 Runtime Edition【Java运行时的版本,也略过】
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 Programming Language【Java编程语言,略过】
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 Development Kit【JDK的介绍,略过】
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 Virtual Machine【JVM,也略过,之前学习已经对它有了解过了】
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. (1) The Java Virtual Machine
Exploring the JVM Architecture【探索JVM架构】
Hotspot Architecture【Hotspot架构】
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 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.”
HotSpot虚拟机它会处理一种架构:支持一个很强特性基础和能力,并且支持实现高性能和可伸缩性的能力。
“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.”比如说HotSpotJVM的既时编译器可以生成动态优化。换句话说,他们会在Java应用运行时做出一些优化决策,同时面向底层系统架构会生成高性能的底层机器指向。【很显然底层的代码运行效果是最高的嘛】
“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虚拟机既便是在大型的计算机系统当中也能得到很高的伸缩性。
】
The main components of the JVM include the class loader, the runtime data areas, and the execution engine.
JVM主要的组成部分包含了类加载器【这也是为啥之前我们在学习JVM时对于类加器花了很多时间去学习,因为它在JVM体系中扮演着非常重要的角色】、运行时数据区域和执行引擎。
Key Hotspot Components【Hotspot关键组件】
The key components of the JVM that relate to performance are highlighted in the following image.
与性能相关的JVM的关健组件在下面的图中已经被高度出来了,如Heap、JIT Compiler、Garbage Collector。
【解读:关于此图其实在之前的学习中已经见过了,回顾一下:
是不是其实这些信息都是来自于官网,所以。。】
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.
【解读:
“There are three components of the JVM that are focused on when tuning performance. The heap is where your object data is stored. ”
在JVM中有三个组件重点是放在了性能调优上面, 堆是你的对象数据存储的位置。
“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.”
这个区域接下来是由在启动时所选择的垃圾收集器来管理,大多数调优的选项都是跟调整堆的大小以及为你当前的场景选择最为适合的垃圾收集器。【换句话来说就是说jvm的性能调优主要是两部分,一是调整堆的大小,二是选择最符合当前场景的垃圾收集器】
“The JIT compiler also has a big impact on performance but rarely requires tuning with the newer versions of the JVM.”
既时编译器也会对程序性能产生非常大的影响,但是对于最新版本的JVM是很少需要对它进行调优的。【换言之,也就是其实JTI底层已经对性能做得比较到位了,不需要我们再对它进行任何性能的调优了】
】
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应用进行调优时,其主要是聚焦到这两个主要目标中的其中一个:响应能力和吞吐量。我们会随着教程的进行重新来介绍这些概念。
【其实这两块在之前的理论中也已经说过了,回顾一下:
下面再以官方的角度再来解读一下这俩概念。】
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
一个桌面UI到底有多快能响应一个事件。- 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.
对于关注响应能力的应用来说,长时间的暂时是无法接受的。它的关注点是在短时间内能够及时响应上面。
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.
高暂停的时间对于一个关注于吞吐量的应用来说是可以接受的。由于高吞量的应用关注的是长时间的大量任务的执行,快速响应并不值得考虑。
好,接下来关注下一小节“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:
【解读:“The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories.”
G1垃圾收集器是一个服务端的垃圾收集器,它针对的是拥有较大内存的多处理器的机器。
“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垃圾收集器是在Oracle的JDK7 update 4及以后的版本得到了完全的支持。G1垃圾收集器是针对这样的应用来进行设计的:
】
- Can operate concurrently with applications threads like the CMS collector.
能够像CMS收集器一样跟应用线程并发的去执行。【CMS本身就是一个并发的收集器,也就是GC线程与应用线程可以同时间执行】- Compact free space without lengthy GC induced pause times.
可以压缩可用的空间,而不会让GC引起暂停时间过长。- Need more predictable GC pause durations.
需要更多的可预测的GC暂停的间隔。【也就是说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 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. ”
G1从长远的计划是要替换掉CMS,将G1和CMS进行比较,有一些区别使得G1成为一个更加好的解决方案。
“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. ”
其中一个区别就是G1是一个压缩的收集器,G1充分的进行压缩来达到完成避免细粒度的free lists分配的使用,同时还依赖于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.”
这样能极大的简化收集器的部分,并且可以极大的消除潜在碎片的问题,此外,相比CMS收集器来说,G1还提供了更多可预计垃圾收集暂停的时间,并且可以让用户来指定它所暂停的目标。
】
G1 Operational Overview【G1操作概览】
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.
对于以前垃圾收信器(像serial、parallel、CMS)都是将堆划分为三个部分:年轻代、老年代、固定内存大小的永久代。
上面这张图也是在之前理论学习中接触过的,回顾一下:
All memory objects end up in one of these three sections.
所有的内存对象最终都会被放置到这三个区域当中的一个。
The G1 collector takes a different approach.
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.
【解读:
“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. ”
某些区域的集合会被分配成就像传统的收集器一样相同的角色(如eden、survivor、old),但是对于它们来说并没有指定一个固定的大小的空间。
“This provides greater flexibility in memory usage.”
这就提供了内存使用当中最大的灵活性。
】
FAQ:这句话是不是有点前后矛盾,如:
其实是这样的:“equal-sized heap regions”是指图中的小方格,这是固定大小的:
而“but there is not a fixed size ”是指的比如Eden空间一定得要占10M,Survivor一定是要占3M,Old一定得要占100M,有可能Eden过一段时间占8M,也有可能是占10M,也有可能占3M,指的是针对每一个角色所占用的大小。
关注个人公众号,获得实时推送