Java front-end compilation and back-end compilation understanding

Guided reading

The process of translating source code into object code is called compilation.

Here again explain what JDK, JRE, and JVM are:

JDK is a java development kit, including: bin, db, include, jre, and ilb files. in:

bin is the JDK compiler (javac.exe); db is the built-in database; Include is the header file for the interaction between java and JVM; Jre is the environment in which java runs, and the bin file in the jre file can be seen as jvm, lib file It is the class library required by the jvm to work. The jvm and lib are called jre.

1. Front-end compilation

        Java front-end compilation refers to the process of converting to .*.java文件*.class文件

advantage:

Many new features of Java syntax ("syntactic sugar": generics, inner classes, etc.) are implemented by front-end compilers rather than virtual machines;

The compiled Class file can be directly interpreted and executed by the JVM interpreter, saving compilation time and speeding up the startup speed;

shortcoming:

There are almost no optimization measures for the efficiency of the code;

The interpretation and execution efficiency is low, so it needs to be combined with the following JIT compilation;    

Front-end compilers : Oracle javac, Incremental Compiler (ECJ) in Eclipse JDT, etc.;

The front-end compilation process is as follows:

2. Backend compilation

         Back-end compilation is also known as just-in-time compiler,  through the Java Virtual Machine (JVM) built-in just-in-time compiler (Just In Time Compiler, JIT compiler), at runtime (file loading in memory) The process of compiling the bytecode of the Class file into native machine code;            

2.1 Why do you need back-end compilation (just-in-time compilation)?

        Java programs are initially executed through the interpreter, explaining a sentence and executing a sentence, which is very inefficient. When it is found that a method or code block is run very frequently, that is, when the bytecode is executed multiple times and needs to be repeatedly translated into native code, we can consider these codes as hot codes. In order to improve the running efficiency of these hot codes, virtual There is an opportunity to compile the hot code into local machine code and optimize it. When the write-only code needs to be executed again, it can be called directly without compiling or interpreting it again. So there is just-in-time compilation.

2.2 What is considered a hot code?

        A threshold is set in the JVM. When the number of calls of a method or code block in a certain period of time exceeds this threshold, it will be compiled and stored in the codeCache. When the code is encountered next time, the machine code will be read from the codeCache and executed directly to improve the performance of the program. The overall execution process is roughly as follows:

The back-end compilation is described as follows: 

Reference article link address:

Java | Let's talk about the compilation process (compile front end & compile back end) - Programmer Sought

                                  Basic Skills | Java Just-In-Time Compiler Principle Analysis and Practice - Meituan Technical Team

Guess you like

Origin blog.csdn.net/qq_35207086/article/details/123758442