Advanced Java-02a-JVM简介

JVM Architecture Explained

The JVM Architecture Explained - DZone Java

JVM 分为三个子系统: Class Loader, Runtime Data Area, Execution Engine

(1)ClassLoader

ClassLoader:Java’s dynamic class loading functionality is handled by the ClassLoader subsystem. It loads, links. and initializes the class file when it refers to a class for the first time at runtime, not compile time.

  1. BootStrap ClassLoader – Responsible for loading classes from the bootstrap classpath, nothing but rt.jar. Highest priority will be given to this loader.
  2. Extension ClassLoader – Responsible for loading classes which are inside the ext folder (jre\lib).
  3. Application ClassLoader –Responsible for loading Application Level Classpath, path mentioned Environment Variable, etc.

The above ClassLoaders will follow Delegation Hierarchy Algorithm while loading the class files.

(2)Runtime Data Area

Runtime Data Area:…

  1. Method Area
  2. Heap Area
  3. Stack Area
    1. Local Variable Array – Related to the method how many local variables are involved and the corresponding values will be stored here.
    2. Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime workspace to perform the operation.
    3. Frame data – All symbols corresponding to the method is stored here. In the case of any exception, the catch block information will be maintained in the frame data.
  4. PC Registers
  5. Native Method stacks

(3)Execution Engine

Execution Engine: The bytecode, which is assigned to the Runtime Data Area, will be executed by the Execution Engine. The Execution Engine reads the bytecode and executes it piece by piece (// Execution Engine 又分为三个子系统: 解释器, JIT编译器, 垃圾收集器) :

  1. Interpreter: The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required. // 解释器,每次执行同一段代码需要创建多个解释器?
  2. JIT Compiler: Execution Engine 使用 Interpreter解释代码并执行, 当 Execution Engine 检测到重复执行的代码时, JIT编译器将字节码便以为 native code
    1. Intermediate Code Generator: 中间代码生成器–产生中间代码
    2. Code Optimizer: 代码优化器–负责优化上面生成的中间代码
    3. Target Code Generator: 目标代码生成器–负责生成机器代码(或 native code)
    4. Profiler: 负责查找热点,即是否多次调用该方法。
  3. Garbage Collector: 垃圾收集器

【图】JVM Architecture Diagram
JVM-Architecture

HotSpot vs OpenJ9

  • HotSpot VM: Oracle / Sun JDK、OpenJDK的各种变种(例如IcedTea、Zulu),用的都是相同核心的HotSpot VM。
  • OpenJ9 VM: OpenJ9

HotSpot VM 对比 Open J9:

stackOverflow 上的问题:jvm - OpenJDK vs Java HotspotVM - Stack Overflow
OpenJDK VM 和 Oracle Hotspot 是不同的 VM 吗?
Oracle HotSpot VM 基于 OpenJDK HotSpot 项目。因此,它们基本上是同一个 VM,除了 Oracle JVM 有一些额外的商业功能,主要是 Java Flight Recorder, Application Class Data Sharing and Cooperative Memory Management