Java Virtual Machine (JVM) is an imaginary computer that runs Java code. As long as the interpreter is granted to a specific computer based on the JVM specification, any Java code that is compiled can be ensured on that system. This article first briefly describes the process of compiling from the Java file to the final execution, and then describes the JVM specification description.
I. Java source file compile, download, interpret, and execute the development cycle of Java applications include compilation, download, interpretation, and implement several parts. The Java Compiler translate the Java source to the JVM executable code - bytecode. This compilation process is somewhat different from C / C . When the C compiler compiles the code to generate an object, the code is generated in a particular hardware platform. Therefore, during the compilation process, the compiler is converted to a specific memory offset by checking the reference table to a specific memory offset to ensure the program operation. The Java compiler does not compile the variables and methods into a numeric reference, nor is it a memory layout during the execution process, but the symbol reference information is reserved in the bytecode, and the interpreter is created during operation. Memory layout, and then determine the address of a method by checking the table. This ensures that Java portability and safety are guaranteed. The job running the JVM byte code is done by the interpreter. Explain the execution process divided into three parts: the implementation of the code, the execution of the checksum code of the code. The work of the load code is done by "Class loader". The class loader is responsible for loading all the code required to run a program, which also includes the class inherited in the program code and the class being called by it. When the class loader is loaded into a class, the class is placed in its own namespace. In addition to category, there is no other way between the class, and there is no other way to influence other classes. All classes on this desk are in the same address space, while all classes introduced from the outside have their own independent namespace. This allows local categories to achieve higher operating efficiency by sharing the same namespace, while ensuring that they do not interact with each other from the outside. When all classes needed to run the program, the interpreter can determine the memory layout of the entire executable program. The interpreter is based on the symbol to establish a correspondence and query table with a specific address space. By determining the memory layout of the code at this stage, Java solves the problem of crashing by superclass changes and the subclass of the subclass, but also prevents the code for illegal access to the address. Subsequently, the loaded code is checked by the bytecode verifier. The verifier can find a variety of errors such as operational counting overflow, illegal data type transformation. After verification, the code starts to be executed. There are two ways to perform the Java bytecode: 1. Instant compilation mode: The interpreter first compiles the byte code into a machine code, then execute the machine code. 2. Explanation Execution: The interpreter completes all the operations of the Java byte code program by each interpretation and executing a small piece of code. Usually used a second method. Since the JVM specification describes sufficient flexibility, this has higher efficiency to translate bytecode to machine code. For applications that require high running speeds, the interpreter can instantly compile the Java bytecode as a machine code, which greatly guarantees the portability and high performance of Java code.
II .JVM Specifications Description JVM Design Goal is to provide a computer model based on abstract specifications, to interpret programs developer's high flexibility, and ensure that Java code can run on any system that meets this specification. JVM gives specific definitions to certain aspects of its implementation, especially for Java executable code, namely byte code (bytecode), a clear specification. This specification includes the syntax and values of the opcode and the operand, the value of the identifier, and the Java object in the Java class file, the constant buffer pool in the JVM storage image. These definitions provide the information and development environments required for JVM interpreter developers. Java designers want to give developers to use Java's freedom to use Java. JVM defines five specifications for controlling Java code interpretation execution and concrete implementation, which is: JVM instruction system JVM register JVM stack structure JVM Debris Recycling JVM Storage Area 2.1JVM instruction system JVM instruction system is extremely similar to other computers. The Java directive is also composed of two parts: operation code and operand. The operation code is 8-bit binary number, and the operands are followed behind the operation code, and the length is different depending on the needs. The operating code is used to specify the nature of a command operation (here we use the form of assembly symbols), such as the iLoad is loaded from the memory, ANEWARRAY is represented as a new array allocation space, IAND indicates two integers " With ", RET is used for flow control, indicating that it is returned from the call to a certain method. When the length is greater than 8 bits, the operand is divided into two or more bytes. The JVM uses "BIG Endian" encoding mode to handle this situation, that is, the high bits stores in the low byte. This is consistent with the encoding method employed with Motorola and other RISC CPUs, and the "Little Endian" encoding method used by Intel is different from the low bits to the low BITS. The Java instruction system is designed with the implementation of the Java language, which includes instructions for calling methods and monitoring multi-step systems. The length of the 8-bit opcode of Java has a maximum of 256 instructions in JVM, and more than 160 operas have been used. 2.2JVM instruction systems All CPUs contain register groups for saving the system status and processor information. If the virtual machine defines a larger register, you can get more information without having to access stacks or memory, which facilitates increasing the running speed. However, if the registers in the virtual machine are more than the actual CPU registers, the processor will take up a lot of time to use a regular memory analog register when implementing the virtual machine, which will reduce the efficiency of the virtual machine. In this case, JVM only sets four most common registers. They are: PC Program Counter OPTOP Operation Noth Stack Top Total Frame Current Environment Pointer VARS Pointer All registers in the current execution environment All registers are 32 bits. PC is used to record execution of the program. OPTOP, FRAME and VARS are used to record pointers to the Java stack area. 2.3JVM stack structure As a stack-based computer, Java stack is the main method of JVM storage information. When the JVM gets a Java bytecode application, create a stack frame for each of the classes in this code to save the status information of the method. Each stack frame includes the following three types of information: local variable execution environment operation count stack local variable for storing local variables used in a class method. The VARS register points to the first partial variable in this variable table. The execution environment is used to save the interpreter to explain the information required for the Java bytecode. They are: the last call method, local variable pointer and operator stack top and stack pointer. The execution environment is a control center that performs a method.
For example, if the interpreter is to perform IADD (integer addition), first find the current execution environment from the Frame register, then find the operator stack from the execution environment, from the top of the stack, two integers, the addition, finally Into the top. Operation Stack The result of the number of operands and operations required to store operations. 2.4JVM Debris Recycling Heap Java Class The storage space required is assigned on the heap. The interpreter is specifically responsible for the work of class instance allocation space. After the interpreter is assigned to an instance, the use of the memory area occupied by the instance is started to record the instance. Once the object is used, it will be reclaimed into the heap. In the Java language, there is no other way to apply and release memory in addition to the new statement. The work that releases and recovery of memory is taken by the Java running system. This allows the Java operating system to determine the method of fragmentation. In the Java interpreter developed by Sun, the fragmentation is executed in a manner in the manner. This not only provides a good performance for the operation system, but also enables program designers to get rid of their own risk of memory usage. 2.5JVM storage area JVM has two types of storage: constant buffer pools and method areas. Constant buffer pools are used to store class names, methods, and field names, as well as crux constance. The method area is used to store the bytecode of the Java method. The specific implementation of these two storage areas is not clear in the JVM specification. This makes the storage layout of the Java application must be determined during operation, depending on the implementation of the specific platform. The JVM is a specific platform defined for the Java bytecode, which is the basis of the independence of the Java platform. The current JVM has some restrictions and insufficient, which is still improved, but in any case, JVM's thoughts are successful. Comparative analysis: If Java original program is imagined into our C original program, the bytecode generated after the Java original program is equivalent to the 80x86 machine code after C original program (binary file), the JVM virtual machine is equivalent to 80x86 computer system, Java interpreter equivalent to 80x86cpu. The machine code is run on the 80x86CPU, running on the Java interpreter, a Java bytecode. The Java interpreter is equivalent to "CPU" running the Java bytecode, but the "CPU" is not implemented by hardware, but is implemented in software. The Java interpreter is actually an application under a particular platform. Just implement the interpreter program under specific platforms, the Java bytecode can run under the platform by the interpreter program, which is the foundation of the Java cross-platform. Currently, it is not a corresponding Java interpreter program under all platforms, which is also the reason for Java to run under all platforms, which can only run under the platform that has implemented the Java interpreter program.