G1垃圾收集器官方文檔透徹解讀【官方解讀】

在前幾回中已經對G1的理論進行了一個比較詳細的瞭解了,對於G1垃圾收集器最權威的解讀確定得上官網,當我們將官網的理解透了,那基本上網上對於G1的說明其實最終都是來自於官網,因此接下來會詳細來解讀Oracle官網對於G1垃圾收集器的權威說明,其網址爲:「https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html」,打開以下:html

先來解讀一下大綱:java

好,接下來開啓第一小節的解讀「Overview」,因爲是概覽,因此比較簡單,大概瞭解下既可。git

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

解讀
「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的一些命令行的開關,以及用於日誌操做的一些選項。express

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

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,指的是針對每個角色所佔用的大小。

相關文章
相關標籤/搜索