First question: Do you know the difference in how Java and C + + work?
Java relies on the runtime environment of the JRE, while the C + + code does not require additional runtimes.
So the question comes again, since C + + is running in a very mature way, then why is Java running in a virtual machine?
- To achieve the goal of writing once, running around.
- The JVM, as a good hosting environment, can replace us in some of the tedious and error-prone parts of code, such as garbage collection.
How does a Java virtual machine run Java bytecode?
From the virtual machine's point of view, executing Java code first needs to load its compiled class file into a Java virtual machine. The loaded Java class is stored in the method area. The virtual opportunity executes the code of the method area when it is actually run.
During the run, whenever a call enters a Java method, the Java virtual opportunity generates a stack frame in the Java method stack of the current thread to hold the local variable and the operand of the bytecode. The size of this stack frame is calculated in advance, and the Java Virtual Machine does not require the stack frame to be continuously distributed in the memory space.
In the hotspot, the translation process has two forms: the first is the interpretation of execution, that is, the bytecode is translated into machine code and executed, the second is the instant compile (JIT), a method contains all the bytecode compiled into machine code and then executed.
The advantage of the former is that there is no need to wait for compilation, while the latter's advantage is that it actually runs faster. hotspot defaults to mixed mode, which combines the benefits of both interpretation execution and instant compilation. He will first explain the execution of the bytecode, and then the repeated execution of the hot code in the method of the immediate compilation of the unit.
How efficient is the Java virtual machine running?
Instant compilation is based on the assumption that the program conforms to the 28 law, that is, 20% of the code occupies 80% of the computing resources.
For most of the infrequently used code, we don't need to compile it into machine code in time, but run it in a way that interprets execution, and on the other hand, we can compile it into machine code to achieve the desired speed for the hot spot code that occupies only a small fraction.
In theory, the execution efficiency of an instant compiled Java program can be more than C + +. This is because, in contrast to static compilation, the runtime information of a program is immediately compiled and can be optimized based on this information. For a chestnut: for a virtual method call, although there are many target methods, he may only invoke one of them during the actual run. This information can be used by the instant compiler to circumvent the overhead of virtual method calls.
The JVM learns how the-java code works