Java Virtual Machine 简明教程

JVM (Java Virtual Machine) Architecture

What is JVM (Java Virtual Machine)?

JVM (Java Virtual Machine) 是一个虚拟机,一个有自己的 ISA、内存、栈、堆等的抽象计算机。它在主机操作系统上运行,并在其上提出对资源的需求。

The JVM (Java Virtual Machine) is a virtual machine, an abstract computer that has its own ISA, memory, stack, heap, etc. It runs on the host OS and places its demands for resources on it.

JVM (Java Virtual Machine) 是规范并且可以有不同的实现,只要它们符合规范。规范可以在以下链接中找到 − https://docs.oracle.com

The JVM (Java Virtual Machine) is a specification and can have different implementations, as long as they adhere to the specs. The specs can be found in the below link − https://docs.oracle.com

Oracle 有自己的 JVM 实现(称为 HotSpot JVM),IBM 也有自己的实现(例如 J9 JVM)。

Oracle has its own JVM implementation (called the HotSpot JVM), the IBM has its own (the J9 JVM, for example).

规范中定义的操作如下(来源 − Oracle JVM 规范)

The operations defined inside the spec are given below (source − Oracle JVM Specs)

  1. The 'class' file format

  2. Data types

  3. Primitive types and values

  4. Reference types and values

  5. Run-time data areas

  6. Frames

  7. Representation of objects

  8. Floating-point arithmetic

  9. Special methods

  10. Exceptions

  11. Instruction set summary

  12. Class libraries

  13. Public design, private implementation

JVM (Java Virtual Machine) Architecture

HotSpot JVM 3 的架构如下 −

The architecture of the HotSpot JVM 3 is shown below −

architecture

执行引擎包括垃圾收集器和 JIT compiler。JVM 有两种版本 - clientserver。它们都共享相同的运行时代码,但是使用的 JIT 不同。稍后我们将详细了解这点。用户可以通过指定 JVM 标志 -client 或 -server 来控制使用什么版本。服务器 JVM 旨在用于服务器上的长时间运行的 Java 应用程序。

The execution engine comprises the garbage collector and the JIT compiler. The JVM comes in two flavors − client and server. Both of these share the same runtime code but differ in what JIT is used. We shall learn more about this later. The user can control what flavor to use by specifying the JVM flags -client or -server. The server JVM has been designed for long-running Java applications on servers.

JVM 有 32 位和 64 位版本。用户可以通过在 VM 参数中使用 -d32 或 -d64 指定要使用的版本。32 位版本只能寻址最多 4G 的内存。随着关键应用程序在内存中维护大型数据集,64 位版本满足了这一需求。

The JVM comes in 32b and 64b versions. The user can specify what version to use by using -d32 or -d64 in the VM arguments. The 32b version could only address up to 4G of memory. With critical applications maintaining large datasets in memory, the 64b version meets that need.

Components of JVM (Java Virtual Machine) Architecture

以下是 JVM(Java 虚拟机)架构的主要组件:

The following are the main components of JVM (Java Virtual Machine) architecture:

1. Class Loader

JVM 以动态方式管理加载、链接和初始化类和接口。在加载过程中,JVM finds the binary representation of a class and creates it.

The JVM manages the process of loading, linking and initializing classes and interfaces in a dynamic manner. During the loading process, the JVM finds the binary representation of a class and creates it.

在链接过程中,loaded classes are combined into the run-time state of the JVM so that they can be executed during the initialization phase。JVM 主要使用存储在运行时常量池中的符号表进行链接过程。初始化实际上包括 executing the linked classes

During the linking process, the loaded classes are combined into the run-time state of the JVM so that they can be executed during the initialization phase. The JVM basically uses the symbol table stored in the run-time constant pool for the linking process. Initialization consists of actually executing the linked classes.

以下是类加载器的类型:

The following are the types of class loaders:

  1. BootStrap class loader: This class loader is on the top of the class loader hierarchy. It loads the standard JDK classes in the JRE’s lib directory.

  2. Extension class loader: This class loader is in the middle of the class loader hierarchy and is the immediate child of the bootstrap class loader and loads the classes in the JRE’s lib\ext directory.

  3. Application class loader: This class loader is at the bottom of the class loader hierarchy and is the immediate child of the application class loader. It loads the jars and classes specified by the CLASSPATH ENV variable.

2. Linking and Initialization

链接过程包括以下三个步骤:

The linking process consists of the following three steps −

  1. Verification − This is done by the Bytecode verifier to ensure that the generated .class files (the Bytecode) are valid. If not, an error is thrown and the linking process comes to a halt.

  2. Preparation − Memory is allocated to all static variables of a class and they are initialized with the default values.

  3. Resolution − All symbolic memory references are replaced with the original references. To accomplish this, the symbol table in the run-time constant memory of the method area of the class is used.

Initialization 是类加载过程的最后阶段。静态变量被分配原始值,并且执行静态块。

Initialization is the final phase of the class-loading process. Static variables are assigned original values and static blocks are executed.

3. Runtime Data Areas

JVM 规范定义了程序执行期间所需的某些运行时数据区域。其中一些在 JVM 启动时创建。其他一些是线程局部的,并且仅在创建线程时创建(并在线程被销毁时销毁)。这些如下所示:

The JVM spec defines certain run-time data areas that are needed during the execution of the program. Some of them are created while the JVM starts up. Others are local to threads and are created only when a thread is created (and destroyed when the thread is destroyed). These are listed below −

它是每个线程的局部变量,并且包含线程当前正在执行的 JVM 指令的地址。

It is local to each thread and contains the address of the JVM instruction that the thread is currently executing.

它是每个线程的局部变量,并且在方法调用期间存储参数、局部变量和返回地址。如果线程需要的堆栈空间超过允许的最大值,可能会发生 StackOverflow 错误。如果堆栈可以动态扩展,它仍然可以引发 OutOfMemoryError。

It is local to each thread and stores parameters, local variables and return addresses during method calls. A StackOverflow error can occur if a thread demands more stack space than is permitted. If the stack is dynamically expandable, it can still throw OutOfMemoryError.

它是所有线程共享的,并且包含运行时创建的对象、类元数据、数组等。它在 JVM 启动时创建,并在 JVM 关闭时销毁。你可以使用某些标志控制你的 JVM 从操作系统请求的堆大小(稍后会详细介绍)。必须小心不要请求过少或过多的内存,因为这会产生重要的性能影响。此外,GC 管理此空间,并会不断删除死对象以释放空间。

It is shared among all the threads and contains objects, classes' metadata, arrays, etc., that are created during run-time. It is created when the JVM starts and is destroyed when the JVM shuts down. You can control the amount of heap your JVM demands from the OS using certain flags (more on this later). Care has to be taken not to demand too less or too much of the memory, as it has important performance implications. Further, the GC manages this space and continually removes dead objects to free up the space.

此运行时区域对所有线程都是通用的,并且在 JVM 启动时创建。它存储每类结构,例如常量池(稍后会详细介绍)、构造函数和方法的代码、方法数据等。JLS 未指定是否需要对该区域进行垃圾回收,因此 JVM 的实现可能会选择忽略 GC。此外,这可能会或可能不会根据应用程序的需求进行扩展。JLS 没有强制规定任何相关内容。

This run-time area is common to all threads and is created when the JVM starts up. It stores per-class structures such as the constant pool (more on this later), the code for constructors and methods, method data, etc. The JLS does not specify if this area needs to be garbage collected, and hence, implementations of the JVM may choose to ignore GC. Further, this may or may not expand as per the application’s needs. The JLS does not mandate anything with regard to this.

JVM 维护一个每类/每类型数据结构,它在链接已加载的类时充当符号表(它扮演的众多角色之一)。

The JVM maintains a per-class/per-type data structure that acts as the symbol table (one of its many roles) while linking the loaded classes.

当一个线程调用一个本地方法时,它就会进入一个新世界,其中 Java 虚拟机的结构和安全限制不再妨碍它的自由。本地方法可能会访问虚拟机的运行时数据区域(它取决于本地方法接口),但也可能执行它想要做的任何其他操作。

When a thread invokes a native method, it enters a new world in which the structures and security restrictions of the Java virtual machine no longer hamper its freedom. A native method can likely access the runtime data areas of the virtual machine (it depends upon the native method interface), but can also do anything else it wants.

4. Execution Engine

执行引擎负责执行字节码,它有三个不同的组件:

The execution engine is responsible for executing the bytecode, it has three different components:

JVM 管理 Java 中对象的整个生命周期。一旦创建了一个对象,开发人员就不需要再担心它了。如果对象变成死对象(即不再引用它),它将通过串行 GC、CMS、G1 等多种算法之一从堆中弹出。

The JVM manages the entire lifecycle of objects in Java. Once an object is created, the developer need not worry about it anymore. In case the object becomes dead (that is, there is no reference to it anymore), it is ejected from the heap by the GC using one of the many algorithms – serial GC, CMS, G1, etc.

在 GC 过程中,对象在内存中移动。因此,在该进程进行时,不能使用那些对象。整个应用程序必须在整个进程持续时间内停止。这种暂停被称为“停止世界”暂停,并且开销很大。GC 算法的首要目标是减少此时间。

During the GC process, objects are moved in memory. Hence, those objects are not usable while the process is going on. The entire application has to be stopped for the duration of the process. Such pauses are called 'stop-the-world' pauses and are a huge overhead. GC algorithms aim primarily to reduce this time.

解释器解释字节码。它能快速解释代码,但执行速度较慢。

The interpreter Interprets the bytecode. It interprets the code fast but it’s slow in execution.

JIT 表示 Just-In-Time。JIT 编译器是 Java 运行时环境的主要部分,它在运行时编译字节码为机器码。

The JIT stands for Just-In-Time. The JIT compiler is a main part of the Java runtime environment and it compiles bytecodes to machine code at runtime.

5. Java Native Interface (JNI)

Java 本机接口 (JNI) 与对执行至关重要的本机方法库进行交互。

Java Native Interface (JNI) interacts with the native method libraries which are essential for the execution.

6. Native Method Libraries

本机方法库是 C 和 C++ 库(本机库)的集合,对执行至关重要。

Native method libraries are the collection of C and C++ libraries (native libraries) which are essential for the execution.