JVM and Java Architecture

Preface

There is no best programming language in the world, only the programming language most suitable for specific application scenarios.

Introduction to Java and JVM

1、Java

Java: a cross-platform language
Insert picture description here

2、JVM

JVM: a cross-language platform
Insert picture description here

With the official release of Java7, the designers of the Java virtual machine have basically implemented the JSR-292 specification to run programs written in non-Java languages ​​on the Java virtual machine platform.
The Java virtual machine does not care at all what programming language the program running in it is written in, it only cares about the "bytecode" file. In other words, the Java virtual machine has language independence, and will not simply be "bound for life" with the Java language, as long as the compilation results of other programming languages ​​meet and include the internal instruction set, symbol table and other auxiliary information of the Java virtual machine , It is a valid bytecode file, which can be recognized by the virtual machine and loaded and run.

3. Bytecode

The java bytecode we usually say refers to the bytecode compiled in the java language. To be precise, any bytecode format that can be executed on the JVM platform is the same. So it should be collectively referred to as: jvm bytecode .
Different compilers can compile the same bytecode file, and the bytecode file can also run on different JVMs.
The Java virtual machine is not necessarily related to the Java language. It is only associated with a specific binary file format—the class file format. The class file contains the Java virtual machine instruction set (or called bytecode, Bytecodes) and symbol table. , And some other auxiliary information.

Virtual Machine and Java Virtual Machine

1. Virtual machine

The so-called virtual machine (Virtual Machine) is a virtual computer. It is a
piece of software used to execute a series of virtual computer instructions. In general, virtual machines can be divided into system virtual machines and program virtual machines.
The well-known Visual Box, VMware belong to the system virtual machine, which is a simulation of a physical computer and provides a software platform that can run a complete operating system. The typical representative of the program virtual machine is the Java virtual machine, which is specifically designed to execute a single computer program. The instructions executed in the Java virtual machine are called Java bytecode instructions. Whether it is a system virtual machine or a program virtual machine, the software running on it is limited to
the resources provided by the virtual machine .

2. Java virtual machine

The Java virtual machine is a virtual computer that executes Java bytecode. It has an independent operating mechanism, and the Java bytecode that it runs may not be compiled from the Java language.
The various languages ​​of the JVM platform can share the cross-platform nature, excellent garbage collector, and reliable just-in-time compiler brought by the Java virtual machine.
The core of Java technology is the Java virtual machine (JVM, Java virtual machine), because all Java programs run inside the Java virtual machine.

The role of the Java virtual machine

The Java virtual machine is the operating environment of the binary bytecode, which is responsible for loading the bytecode into it, and interpreting/compiling it into the execution of machine instructions on the corresponding platform. Each Java instruction pJava virtual machine specification has detailed definitions, such as how to fetch the operand, how to process the operand, and where to put the processing result.

Features of Java Virtual Machine

  • Compile once, run everywhere
  • Automatic memory management
  • Automatic garbage collection function

JVM location

JVM runs on the operating system, it has no direct interaction with the hardware.
Insert picture description here
Insert picture description here

The overall structure of the JVM

HotSpot VM is one of the masterpieces of high-performance virtual machines currently on the market. It uses an architecture in which an interpreter and a just-in-time compiler coexist. Today, the running performance of Java programmers has long been reborn, and has reached a point where it can compete with c/c++ programs. The Java stack is now called the virtual machine stack.
Insert picture description here
Insert picture description here

Execution engine

The execution engine is one of the core components of the Java virtual machine. Virtual machine is a concept relative to "physical machine". Both machines have code execution capabilities. The difference is that the execution engine of the physical machine is directly built on the processor, cache, instruction set, and operating system levels. The execution engine of the virtual machine is implemented by the software itself, so the structure of the instruction set and execution engine can be customized without being restricted by physical conditions, and
the main task of the JVM is responsible for loading the instruction set format that is not directly supported by the hardware. The bytecode is inside, but the bytecode cannot be directly run on the operating system, because the bytecode instructions are not equivalent to the local machine instructions. It contains only some bytes that can be recognized by the JVM lock. Code instructions, symbol tables and other auxiliary information.
Then, if you want a Java program to run, the task of the execution engine is to interpret/compile bytecode instructions into native machine instructions on the corresponding platform. Simply put, the execution engine in the JVM acts as a translator that translates high-level language into machine language.
From the appearance point of view, the input and output of the execution engine of all Java virtual machines are the same: the input is a byte binary stream, the processing process is the equivalent process of bytecode analysis and execution, and the output is the execution result. What kind of bytecode instructions need to be executed during the execution process completely depends on the PC register. Every time an instruction is executed, the PC register will update the address of the next instruction that needs to be executed. Of course, during the execution of the method, the execution engine may accurately locate the object instance information stored in the Java heap area through the object reference stored in the local variable table, and locate the target object through the metadata pointer in the object header Type information.

Java code execution flow

Java source files are generated by a Java compiler to generate one or more bytecode (.class) files, and each bytecode file corresponds to a specific class. During the source file compilation process, if any compilation link fails, no corresponding points can be generated. Bytecode file. Then the bytecode file is loaded into the memory. Once the bytecode enters the virtual machine, it will be interpreted and executed by the interpreter, or it will be selectively converted into machine code for execution by the instant code generator.
(The operating system only recognizes machine instructions, and the translation of bytecode files into machine instructions is done by the execution engine)
Insert picture description here

JVM architecture model

The instruction stream input by the Java compiler is basically a stack-based instruction set architecture, and the other instruction set architecture is a register-based instruction set architecture.
Specifically: the difference between these two architectures:

1. Based on the characteristics of stack architecture

  • Simpler design and implementation, suitable for resource-constrained systems;
  • Avoid the problem of register allocation: use zero address instruction to allocate.
  • Most of the instructions in the instruction stream are zero-address instructions, and their execution depends on the operation stack. The instruction set is smaller and the compiler is easy to implement.
  • No need for hardware support, better portability and better cross-platform

2. Based on the characteristics of register architecture

  • The typical application is x86 binary instruction set: such as traditional PC and Android Davlik virtual machine.
  • The instruction set architecture is completely dependent on hardware, and portability is poor.
  • It takes less instructions to complete an operation.
  • In most cases, the instruction set based on the register architecture is often dominated by one-address instructions, two-address instructions and three-address instructions, while the instruction set based on the stack architecture is dominated by zero-address instructions.

For example:

   public static void main(String[] args) {
    
    
        int i=2;
        int j=3;
        int  k = i+j;

    }

Execute the above code, the instructions are as follows: stack-based calculation process (taking Java virtual machine as an example):

 public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: iconst_2
         1: istore_1
         2: iconst_3
         3: istore_2
         4: iload_1
         5: iload_2
         6: iadd
         7: istore_3
         8: return
      LineNumberTable:
        line 5: 0
        line 6: 2
        line 7: 4
        line 9: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  args   [Ljava/lang/String;
            2       7     1     i   I
            4       5     2     j   I
            8       1     3     k   I
}

Register-based calculation process

mov eax,2//将eax寄存器的值设为1
add eax,3//使eax寄存器的值加3

JVM life cycle

1. Start the virtual machine

The startup of the Java virtual machine is accomplished by creating an initial class by the bootstrap class loader, which is specified by the specific implementation of the virtual machine.

2. Execution of the virtual machine

A running Java virtual machine has a clear task: execute Java programs. It runs when the program starts, and it stops when the program ends. When executing a so-called Java program, what is really being executed is a process called the Java Virtual Machine.

3. Exit of the virtual machine

There are several situations as follows:

  • The program ends normally
  • The program encountered an exception or error during execution and terminated abnormally
  • The Java virtual machine process is terminated due to an error in the operating system
  • A thread calls the exit method of the Runtime class or the System class, or the halt method of the Runtime class, and the Java security manager also allows this exit or halt operation.
  • In addition, the JNI (Java Native Interface) specification describes the exit of the Java virtual machine when the JNI Invocation API is used to load or unload the Java virtual machine.

Guess you like

Origin blog.csdn.net/weixin_44736475/article/details/108566525