Yes, Java is an interpreted language, but it does not mean that it must be interpreted and executed. Early Stage
But it is found that the efficiency is too low,
Many other virtual machines, such as JIT virtual machines, have emerged because they do not meet various requirements.
Hotspot is similar to a virtual machine. It has been put
JRE 1.3 and later versions.
It is hard to say that Java is interpreted and executed by the virtual machine using the Hot Spot Java virtual machine,
The reason is that hotspot actually compiles bytecode of Java into native code,
Then run.
In fact, in the hotspot virtual machine, two technologies are crucial, that is, dynamic compilation and
Profiling. The compilation of bytecode by hotspot is not pre-compiled before the program runs,
Instead, compile During Run-Time
Dynamic compilation. In fact, just in time also means this.
How does hotspot dynamically compile bytecode of Javad? It adopts a smart approach.
There is a run monitor in hotspot, namely profile Monitor (I do not know how to translate profile in China ),
This module monitors which parts of programs are frequently used and which have critical impact on performance.
Of course, profile monitor has some algorithms. These algorithms may not be perfect, but they are generally better.
Obtain relevant information. It is called a hotspot for codes that affect the running efficiency of programs,
That is, hot spot, hotspot will dynamically compile these departments into machine code, native code,
At the same time, the machine code is also optimized (similar to some optimizations of the C compiler) to improve the running efficiency.
For less-running codes, the hotspot virtual machine no longer wastes time compiling them.
In general, Java bytecode is loaded to the Virtual Machine in an interpreted manner. But Virtual Machine
The analyzer obtains the most influential part of the program efficiency based on a piece of operation, and then uses the dynamic
Compile, optimize at the same time, compile to machine code, and then accelerate the next operation. Total
For example, hotspot has three layers of processing for bytecode: No compilation, and optimization.
For which part of the program is not compiled, which part is compiled, and which part is optimized, the profile
Monitor determines.
So why does Java adopt a dynamic compiler instead of a static compiler like C ++?
The cross-platform running conditions provided by virtual machines are, on the one hand, dynamic compilers are also in many aspects.
Better than static compilers. Profiling is an example. Static compilers are usually hard to be accurate.
Determine which part of the program needs optimization most. Although the static compiler can
All code is compiled into native code, but it cannot be optimized like the dynamic compiler.
Another typical example is method inlining. We know that
In Java, function calling is a waste of system time, because there are many stack-and-out operations.
Therefore, an optimization method is to change the original function call, compiled by the compiler,
For non-function calls, function code is directly embedded into the call and executed in sequence.
However, this method is difficult to implement in the compiler of an object-oriented language such as Java/C ++.
For static compilers, you can usually perform method inlining on private, static, and other functions,
However, these object-oriented languages support function overloading and support dynamic concatenation (I don't know if this is the case ).
Overridden, dynamic binding), so the static compiler does not know exactly
The implementation of the function is given to inline.
Due to the monitoring of function calls, the dynamic compilation of hotspot can accurately know some environments.
How can the overloaded and dynamically recognized functions be transferred to the caller by inline?
In fact, for some server applications, the efficiency can be greatly improved.
Hotspot actually has two versions: server and client. But their structure
It is the same as the essence, but the optimization is different in some places.
After learning about this, we know that sometimes Java programs can even run faster than C Programs.