JVM summary three-bytecode, bytecode instructions, JIT compilation and execution
The class file compiled by the Java file, the middle layer of the Java cross-platform, JVM through the bytecodeExplain execution(Execution mode, andJIT compilation and executionTo block operating system dependencies.One byte(8 bits) can be storedDifferent commands in 256Such commands are bytecode. About 200 Java commands constitute a bytecode file (. class ).
I. Main instructions of bytecode:
The hexadecimal file in the. Class file (for example, figure 1 ).Cafe babeIt indicates that this file is a compiled Java file. 00000034 represents the version number, and 01670700 a byte (8 bits) is a byte command. Because numerical commands are too ugly to understand, we can useJavapTranslate the command into a note (for example, figure 2 ).
Figure 1
Figure 2
1. load or store commands
In the stack frame, data is transmitted between the local variable table and the operation stack through commands.
- Iload, aload: Push int and object reference types from the local variable table to the top of the Operation stack;
- Istore, astore: Store int and object reference types from the top of the Operation stack to the local variable table;
- Iconst, bipush, sipush, LDC: Load the always bright light to the top of the Operation stack.
2. Operation commands
Calculate the value on the Operation stack and write the result to the top of the Operation stack, as shown in figureIadd, imul.
3. type conversion commands
I2L, D2F
4. Object creation and access commands
New
In addition to bytecode commands, it also includes matching the linenumber storage bytecode with the source code to facilitate debugging and positioning. localvariable stores the local table used by the current method.
Ii. process of converting a Java source code file into a bytecode (. Class) File
Java source file---------->Lexical Parsing---------->Syntax Parsing---------->Semantic Analysis--------->Generate bytecode--------->Bytecode File
Lexical Analysis: Words and operators are separated by spaces to form the token information stream;
Syntax analysis: Generate a syntax tree based on the token stream and Java syntax specifications;
Semantic Analysis: Check whether the keyword and type match are correct;
Iii. Execution Mode
1. Explain the execution
The JVM executes the code by loading the bytecode;
2. JIT compilation and execution
Translate hotspot Code (for example, high-frequency method body, loop body, and public module) into machine code to improve future execution efficiency;
3. Mixed JIT compilation execution and interpretation execution (mainstream JVM Execution Mode)
During each method call, the counter of the method call is incremented by 1. If the Count reaches the threshold, the request is compiled into a machine code, and the machine code is placed in the Code cache. In the next execution, check whether the machine code has been compiled into the machine code, the compiled machine code is directly executed. If the machine code is not compiled, it is interpreted for execution (that is, the execution Byte Code );
JVM summary three-bytecode, bytecode instructions, JIT compilation and execution