什么是JVM

Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java Code or applications. It converts Java bytecode into machines language. JVM is a part of Java Runtime Environment (JRE). In other programming languages, the compiler produces machine code for a particular system. However, Java compiler produces code for a Virtual Machine known as Java Virtual Machine.

JVM 包含什么?

  • 由三部分组成:
    • ClassLoader
    • RuntimeMemory/DataArea
    • Execution Engine
    • notion image
      notion image

      类加载器——ClassLoader

      1. 加载机制:按需动态加载,并采用 双亲委派机制
      1. 加载过程:
      1. Loading:加载,getClassLoader()
      1. Linking:链接
        1. Verification:校验阶段,校验文件是否符合 JVM 规范
        2. Preparation:静态变量赋为默认值
        3. Resolution
      1. Initializing:初始化,调用静态代码块,将静态变量赋值为初始值
      1. 分类:
          • Bootstrap:加载核心类,由 C++ 实现,通过 getClassLoader() 方法,结果为 Null
          • Extension:加载扩展包
          • App:加载 classPath 指定的内容
          • Custom ClassLoader:自定义的 ClassLoader
      1. 双亲委派:
      notion image

      运行时数据区域——Runtime Data Area

      1. 由五部分组成:
      notion image
      1. 程序计数器——Program Counter Registers
          • 作用:用于记录线程所执行的字节码的行号
          • 特点:线程私有,所以,同一时刻如果有多个线程,就有多个PC Registers
      1. Java虚拟机栈——JVM Stack Area
          • 当 JVM 中新启动了一个线程,伴随而来的,就是创建一个相应的 虚拟机栈(Stack Area),用于存储局部变量、方法调用、运行结果等
          • 对于线程中的每一个方法调用,都会在被调用时,创建一个 栈帧(Stack Frame),当方法调用执行结束,该 栈帧 就会被销毁
          • 栈帧细分:
            • 局部变量表(Local Variables):变量表的长度,在编译期就已经决定了
            • 操作对象栈(Operand Stack):该栈的深度在编译期就已经决定
            • 帧数据(Frame Data):
      1. 本地方法栈——Native Method Stacks
          • 线程私有,每个线程都有独立的一个本地方法栈
          • 主要是包含了本地方法,指由非 Java 语言编写的方法
      1. Java堆——Heap Area
          • 非线程独有,所以是线程不安全的,不同线程间的方法区和堆共享了相同的内存
          • 主要用来存放Java对象
          • 所谓的 新生代、老年代、永久代,也是在这里
          • GC 也主要是回收 Heap Area 的内存空间
      1. 方法区——Method Area
      All the class level data such as t he run-time constant pool, field, and method data, and the code for methods and constructors, are stored here.
      《Java虚拟机规范》对方法区的约束是非常宽松的,除了和Java堆一样不需要连续的内存和可以选 择固定大小或者可扩展外,甚至还可以选择不实现垃圾收集。

      执行引擎——Execution Engine

      1. 解释器(Interpreter)
      The interpreter reads and executes the bytecode instructions line by line. Due to the line by line execution, the interpreter is comparatively slower.
    • 缺点是:不管一个方法被重复调用多少次,每一次都需要 Interpreter 来解释运行
      1. JIT 编译器(JIT Compiler)
        1. The Execution Engine first uses the interpreter to execute the byte code, but when it finds some repeated code, it uses the JIT compiler. The JIT compiler then compiles the entire bytecode and changes it to native machine code. This native machine code is used directly for repeated method calls, which improves the performance of the system.
          • 组成组件:
            • Intermediate Code Generator:generates intermediate code
            • Code Optimizer:optimizes intermediate code for better performance
            • Target Code Generator:converts intermediate code to native machine code
            • Profiler:finds the hotspots (code that is executed repeatedly)
      1. 垃圾收集器(Garbage Collector)
      The Garbage Collector (GC) collects and removes un referenced objects from the heap area. It is the process of reclaiming the runtime unused memory automatically by destroying them.

      JNI——Java Native Interface

      At times, it is necessary to use native (non-Java) code (for example, C/C++). This can be in cases where we need to interact with hardware, or to overcome the memory management and performance constraints in Java. Java supports the execution of native code via the Java Native Interface (JNI). JNI acts as a bridge for permitting the supporting packages for other programming languages such as C, C++, and so on.

      Native Method Libraries

      Native Method Libraries are libraries that are written in other programming languages, such as C, C++, and assembly. These libraries are usually present in the form of .dll or .so files. These native libraries can be loaded through JNI.

      参考/推荐文章

JUC中的各种锁Leetcode 331 验证二叉树的前序序列化
Loading...