In the previous blog, we introduced theJavaMost of the knowledge points in the field, from the most basicJavassh framework. This should include the java Most of the stuff in the field. However, those knowledge points are for us to understand from an application level ,javajavajava performance optimization in development is a great help, Can make us reduce some unnecessary memory waste and other benefits. So, starting today, I will work with you to learn The contents of the virtual machine. Open from the bottom java< span>
Java Virtual Machine
Java virtual machine (Java VM) referred to as the JVM java virtual machine is an imaginary machine that is implemented by software simulations on a real computer. The Java virtual machine has its own imaginary hardware, such as processor, stack, register, etc., and also has the corresponding instruction system. Let's take a look at the structure of some of the more important Java virtual machines
JVM Registers
All theCpuContains the register groups that are used to hold the system State and the information required by the processor. If a virtual machine defines more registers, it can get more information from it without having to access the stack or memory, which helps to improve the speed of operation. However, if the registers in the virtual machine are more than the actualCpujvm only set the 4pc program counter, Optop Current execution environment pointer, VARs 32pc Used to record the execution of a program. optop,frame and vars used to record points
Jvm stack structure
java jvm The main method of storing information. When jvm get a javavars register points to the first local variable in the variable table. The execution environment is used to save the interpreter to javaiadd (integer addition), first from frame
Jvm fragment recycling heap
javajava language, except java operating system assumed. This allows javasun interpreter and hot Java
JVM Storage Area
JvmThere are two types of storage: a constant buffer pool and a method area. A constant buffer pool is used to store class names, method and field names, and string constants. The method area is used to storeJavaThe byte code of the method. The specific implementation of these two storage areas isJvmThe specifications are not expressly specified. This makesThe storage layout of a Java application must be determined during the run, depending on how the platform is implemented.Jvmis forJavaA specific platform-independent specification for bytecode definitions, which is a JavaFoundation for platform Independence. The currentJvmThere are still some limitations and deficiencies that need to be further perfected, but anyway,JvmThe idea is successful. Comparative analysis: If theJavaThe original program is imagined as ourC++The original program,JavaThe bytecode generated after the original program is compiled is equivalent toC++After the original program compiled80x86Machine code (binary program file),The JVM virtual machine is equivalent to80x86 computer system, the Java interpreter is equivalent to80x86cpu。 In80x86cpuThe machine code is running on theJavaThe interpreter is running on theJavabyte code.JavaThe interpreter is equivalent to runningJavaBYTE-code“cpu" cpu "javajavajavajava interpreter program, which is also javajava The interpreter program is running under the platform.
architecture diagram for Java virtual machines
Java Virtual machine lifecycle from boot to end, when the Java virtual machine starts, in the following cases, the Java VM will end the life cycle:
1. Implementation of the System.exit () method
2. End of normal program execution
3. The program encountered an exception or error during execution and terminated abnormally
4. The Java Virtual machine process terminates due to an operating system error
Java virtual machine stack has three zones : local variable area, run environment area, operand area.
Local variable Area
EachJavaMethod uses a fixed-size set of local variables. They follow theVARsThe character offset of the register to address. Local variables are32Bit of. Long integers and double-precision floating-point numbers occupy two of the space of a local variable,is addressed according to the index of the first local variable. For example , One with index n, so it actually occupies the index and Span style= "font-family:arial;" >n+1) 6464 bit aligned. The virtual machine provides instructions for loading the values in the local variable into the operand stack ,
Operating Environment Area
The information contained in the run environment is used for dynamic linking , normal method returns, and exception snapping.
Operand Stack Area
The machine instruction only takes the operand from the operand stack,operate on them,and return the results to the stack. The reason for choosing a stack structure is:intel486" ,32 bit. It is used to pass parameters to the method , and receive results from methods , also used to support operational parameters ,iadd instruction adds two integers. The added two integers should be the top two characters of the operand stack. The two words are pressed into the stack by the previous instruction. These two integers will pop from the stack, add ,
Each raw data type has special instructions to perform the necessary operations on them. Each operand requires a storage location in the stack,Apart fromLongAndDoubleType, They need two positions. The operand can only be manipulated by an operator that is applicable to its type. For example , press into two int< Span style= "font-family: the song Body;" > The number of types , If you think of them as a longsun virtual machine implementation in , There are a few operations operator dupe and
Local method Stack, when a thread calls a local method, it is no longer constrained by the structure and security constraints of the virtual machine, it can access both the run-time data area of the VM or the local processor and any type of stack. For example, the local stack is a C -language stack, and when the C program calls the C function, the parameters of the function are pressed into the stack in some order, and the result is returned to the calling function. When implementing a Java virtual machine, the local method interface uses the C -language model stack, and its local method stack is scheduled and used exactly the same as the C language stack.
can represent a whole process of running a Java program
3 Operating Procedures for Java virtual machines
The above is a detailed description of the various parts of the virtual machine, the following is a specific example to analyze its operation process.
The virtual machine is started by invoking the method of a specified class, Main, and passed to main A string array parameter, causing the specified class to be loaded, linking other types used by the class, and initializing them. For example, a program:
Class HelloApp
{
public static void Main (string[] args)
{
System.out.println ("Hello world!");
for (int i = 0; i < args.length; i++)
{
System.out.println (Args[i]);
}
}
}
After compiling, type in command-line mode: Java helloapp run virtual machine
helloapp method main to start the Span style= "font-family:arial;" >java virtual machine, passed to main One contains three strings " Machine "helloapp< span>
Started trying to execute The Main method of class HelloApp and found that the class was not loaded, that is, the virtual machine does not currently contain a binary representation of the class, and the virtual machine uses ClassLoader to try to find such a binary representation. If this process fails, an exception is thrown. After the class is loaded and before the main method is called, the class HelloApp must be linked to and initialized with other types. The link consists of three phases: inspection, preparation and resolution. The test examines the symbols and semantics of the loaded main class, prepares to create a static domain of the class or interface, and initializes these fields to the standard default values, and resolves the symbolic references that are responsible for checking the main class to other classes or interfaces, which is optional in this step. The initialization of a class is the execution of a static initialization function declared in a class and the initialization of a static domain. A class must have its parent class initialized before it is initialized. The whole process is as follows:
Reference:http://blog.csdn.net/csh624366188
Deep Java Virtual Machine (i)--java virtual machine underlying structure