Overall ARCHITECTURE diagram of the JVM

Area stack and heap virtual machine multi-thread sharing method and local method stack and the program counter for each thread a unique source code by the compiler into into the bytecode class loader main effect to bytecode file loaded into memory to generate a large class object (stored in a way) the process is divided into initialization loading, links, three stages Each byte code file corresponding to a Java class bytecode is cross-platform file Then the execution engine (including the interpreter and the JIT compiler immediately and garbage collector) the interpreter are explained Only use the interpreter running experience sent some hot spots for repeated execution code Hope compiled in advance that the JIT compiler do the workCopy the code

Java code executes the process

The operating system can only identify machine instructions Can't identify bytecode instructions So you need to perform engine will bytecode interpreter translated into machine code instructions for bytecode line-by-line explanations Ensure the response time JNI repeatedly in the bytecode compiler to execute code (i.e., hot code) directly compiled into machine instructions At the same time in cached approach area Ensure execution performance Mainstream VMS use bothCopy the code

JVM instruction set architecture model

The instruction stream input by Java compiler is one kind of stack-based instruction set architecture and the other kind is register-based instruction set architecture HostSpot. Only PC register, any operation in HostSpot needs to run through the operation stack of the stack and the operation stack of the stack. Thus it can be seen that the HostSpot execution engine architecture is stack-based instruction set architectureCopy the code

The two architectures differ

Stack-based instruction set architecture

  • Easier to design and implement for resource-constrained systems such as embedded small devices set-top boxes, printers, etc
  • Avoid the problem of register allocation by using zero address instruction
  • Most of the instructions in the instruction stream are zero-address instructions whose execution depends on the operation stack. The instruction set is smaller (aligned every 8 bits in a bytecode file) and easier to implement by the compiler
An instruction execution time Need two parts One is the address Another is the operand The so-called zero address instruction Is that there is no address Only the operands An address instruction has an address Two address instruction, there are two address Just a stack into the stack operation Only for data on the top of the stack Other data operation So there is no need to addressCopy the code
  • No hardware support is required (the stack is memory level and does not need to deal with hardware). Portability is better and cross-platform implementation is easier

Register-based instruction set architecture

  • Typical applications are x86 binary instruction sets such as traditional PCS and The Android Davlik VIRTUAL machine
  • The instruction set architecture is completely dependent on hardware (CPU) portability
  • Performance optimization and more efficient execution (CPU execution in the cache)
  • It takes less instruction area to complete an operation
  • In most cases, the instruction set assigned to the register architecture tends to be dominated by one-address, two-address, and three-address instructions (aligned with 16-bit double-byte alignment).

For example 1

The same logic operation 2+3 is performed with the following instructionsCopy the code

The stack-based instruction set architecture has a small instruction set but many instructions to perform the same operation

Iconst_2 // constant 2 pushes istore_1 iconst_3 // Constant 3 pushes istore_2 ILOAD_1 iload_2 iadd // Constant 2, 3 pushes istore_0 // result 5 pushes istore_0Copy the code

Register-based instruction set architecture has a large instruction set and fewer instructions to perform the same operation

Mov eax,2 // add eax,3 // add eax,3 // add eax,3 // add eaxCopy the code

The source code

Compiled bytecode

Decompile

javap -v StackStruTest.class
Copy the code

0, 1, 2 are the actual addresses of the PC registers. After compiling, we can recognize 2+3 is 5, which is the same as writing int I =5 in the source codeCopy the code

If the source code is written like this

Decompile the new bytecode file

Iconst 2 generates constant 2 and stores it. Istore 1 is an index position in the operand stack and stores it in the operand stack of 1. Iconst 3 defines a constant 3. Istore 2 is stored at index 2. Iload_1 iload_2 loads in I and j Iadd does a summation istore_3 and the sum k is stored in the operand stack of 3 and there are 8 lines of instructions corresponding to 3 lines of source codeCopy the code

The main characteristics of stack-based instruction set are: cross-platform, small instruction set and many instructions; Execution performance is worse than register performance