Java-like execution mechanisms can be divided into bytecode interpretation execution and compilation into machine code execution, which can be further divided into client compiler and server Compiler
1. bytecode interpretation execution
JVM is a method of intermediate code. During execution, JVM has its own set of commands. JVM uses invokestatic, invokevirtual, invokeinterface, and invokespecial to find and execute different methods.
Invokestatic will call the static method, invokevirtual will call the method of the object instance, invokeinterface will call the interface method, invokespecial will call the private method and the compiled <init> method, this method is the initialization method of Object Instantiation, as shown in the following code:
Public class demo {public void execute {a.exe cute (); // call invokestatic A = new A (); // call invokespecial. am (); // call invokevirtual ib B = new B (); // call invokespecial B. BM (); // call invokeinterface} public class A {public static string execute () {return "A" ;}public am () {return "am ";}} public interface IB {Public String MB ();} public class B implements IB {Public String MB {return "MB ";}}
The preceding figure shows the corresponding methods of the four commands.
2. Compile and execute
1) Client Compiler (C1)
Client compiler, also known as C1, is suitable for desktop applications because it only performs optimization with a small amount of performance overhead and occupies a small amount of memory, the optimization methods used mainly include inline method, Redundancy Elimination, and de-virtualization.
① Method inline
Generally, a business logic method needs to call multiple methods. The so-called method inline is to directly implant the code in the method body into the current method body, as shown in the following code:
public void m1(){//.....m2();//...}private void m2(){//do.....}
When the number of M2 () bytes is smaller than the specified range during compilation, the M2 () method body is directly implanted into M1 (), as shown below:
public void m1(){//.....//do.....//...}
You can add-XX: + printlnlining to the JDK startup parameter of the debug version to view the inline information of the first off.
② De-Virtualization
It mainly analyzes the class hierarchy. When there is only one implementation class, the method will be implanted into the method body when calling the method, such:
piblic interface IParent(){public void m();}public Child implements Iparent{public void m(){//do.....}}public class Demo{public void ip(Iparent child){child.m();}}
Since only child implements iparent, the final compiled code is:
public void ip(Iparent child){//do....}
③ Redundancy Elimination
Mainly streamlining code and eliminating useless code
For example:
boolean status = false;public void m(){if(status){//do....}
//do..something.......}
After compilation:
public void m(){//do...something....}
2) server Compiler (C2)
C2 occupies a large amount of memory, but uses many optimization techniques. It is optimized based on escape analysis. The so-called escape analysis is a variable in a method, if it has not been referenced by the outside world, it is considered that the change is escape. Otherwise, the escape analysis-based C2 performs scalar replacement, stack allocation, and synchronization elimination.
① Scalar replacement
That is, the variable replacement aggregation volume, such:
B b = new B("Tom mao");System.out.println(b.name);
If B is not used later, the code after the scalar replacement is:
String name = "Tom mao";System.out.println(name);
② Stack allocation
The local variable is directly allocated to the stack. When the method ends, the memory occupied by the scalar will also be released.
③ Eliminate Synchronization
That is, if the synchronization object is not escaped, there is no need for synchronization and the synchronization code will be removed.
By default, Sun JDK selects the corresponding machine code mode based on the machine configuration. When the machine configuration CPU exceeds 2 cores and the memory exceeds 2 GB, C2 is used, however, C1 is always used on Windows machines, and you can add-client or-server to force the configuration at startup.