In-depth study of Java virtual machine

xiaoxiao2021-03-06  157

In-depth study of Java virtual machine

Author: Liu Chao

1 Java technology with Java virtual machines

Speaking of Java, people first think of the Java programming language, but in fact, Java is a technology, which consists of four aspects: Java programming language, Java class file format, Java Virtual Machine and Java Applications (Java API) . Their relationships are shown below:

Figure 1 Java relationship

The operational period represents the Java platform, developers write Java code (.java files), then compile it into a zona code (.class file). The final zode code is loaded into the memory, once the byte code enters the virtual machine, it will be interpreted by the interpreter, or converted into a machine code that is selected by the instant code generator. From the above figure, you can also see that the Java platform is built by Java virtual machines and Java application interfaces. The Java language is channel access to this platform. The program written in Java language and compiles can run on this platform. The structure of this platform is shown below:

In the structure of the Java platform, it can be seen that the Java Virtual Machine (JVM) is in the core position, which is the key to the underlying operating system and hardware. It is a transplant interface, and the transplant interface consists of two parts: the adapter and Java operating system, which depends on the portion of the platform is called the adapter; JVM is realized on a specific platform and operating system through the transplant interface; Java above the JVM is Java Basic class libraries and extended class libraries and their APIs, using Java API written applications (Java Applet) can run on any Java platform without considering the underlying platform, because there is a Java virtual machine ( JVM) implements the separation of the program and the operating system, thereby realizing the Java platform independence.

So what is the Java Virtual Machine (JVM)? Usually we mean when we talk about JVM:

A comparative abstract description of the JVM specification; the specific implementation of JVM; an JVM instance generated during the program operation.

Abstract instructions for JVM specification are some collection of concepts, they have been described in detail in the "The Java Virtual Machine Specification"); the specific implementation of JVM is either software, or The combination of software and hardware has been implemented by many manufacturers and exists above a variety of platforms; the task running the Java program is a single assumption of the JVM's run instance. The Java virtual machine (JVM) we discussed herein is primarily for the third case. It can be seen as an imaginary machine, implemented on the actual computer through software simulation, with hardware that you think, such as processors, stacks, registers, etc., and their own instruction systems.

JVM has a clear task in its living cycle, that is, running the Java program, so when the Java program is started, the JVM is generated; when the program is running, the instance has disappeared. Below we are more in-depth research from the JVM architecture and its running process.

2 Java virtual machine architecture

Just mentioned that JVM can be implemented by different vendors. Since the different manufacturers inevitably lead to some differences in the implementation, JVM can also achieve cross-platform characteristics, which is to be attributed to the architecture when designing JVM.

We know that a JVM instance is not only its own business, and it also involves its subsystem, storage area, data type, and instructions, which describes an abstract internal architecture of JVM, and its purpose is not preferential implementation. When JVM, its internal architecture, more importantly, provides a way to strictly define external behavior when implementing. Each JVM has two mechanisms, one is a class (class or interface) that is loaded with a suitable name, called a sub-load subsystem; another instruction to execute instructions contained in the loaded class or interface called running engines. . Each JVM further includes the five parts of the method area, a pile, Java stack, program counter, and local square stacks, and the architecture of these parts and class load mechanisms and operation engine mechanisms are: Figure 3 JVM architecture

Each instance of JVM has its own methodology and a heap, all threads running in the JVM share these areas; when the virtual machine is loaded, it resolves the class information contained in the binary data. And put them in the method domain; when the program is running, the JVM places all objects initiated by the program on the pile; each thread is created, you will have your own program counter and Java stack, in which program counters are The value refers to the command that is about to be executed, and the Java stack of the thread is stored as the state of the Java method; the state of the local method call is stored in the local square stack, which depends on the specific implementation.

These parts will be described below.

The execution engine is in the core position of the JVM, in the Java virtual machine specification, its behavior is determined by the instruction set. Although the specification is very detailed in detail, what is the implementation of the JVM execution bytecode encounters instructions, but it is very little. The Java virtual machine supports approximately 248 bytes. Each byte code performs a basic CPU operation, for example, add an integer to the register, subroutine transfer, and the like. The Java instruction set is equivalent to the assembly language of the Java program.

The Java instruction set includes a single-byte operator that specifies the operation to be executed, and there are 0 or more operands, the parameters or data required to provide operations. Many instructions do not operate, consisting of only one single-byte operator.

The execution process of the inner layer cycle of the virtual machine is as follows:

Do {

Take an operator byte;

Perform an action according to the value of the operator;

} while (the program is not over)

Due to the simplicity of the instruction system, the process of virtual machine execution is very simple, thereby facilitating improving the efficiency of the execution. The number and size of the operating number in the instruction are determined by the operator. If the operand is larger than one byte, then its stored order is a high byte priority. For example, a 16-bit parameter is stored in two bytes, whose value is:

The first byte * 256 second byte image code code.

The instruction stream is generally only byte alignment. The instruction TableSwitch and Lookup are exception, and the mandatory 4-byte boundary is required within these two instructions.

For local method interfaces, implementation JVM does not require it to have its support, or even completely. Sun Encovery Java Local Interface (JNI) is for portability, of course, we can also design other local interfaces to replace Sun's JNI. However, these design and implementation are more complicated, requiring that the garbage collector does not release those objects that are being called by local methods.

Java's heap is a runtime data area, an instance (object) of the class (object), which is allocated, and its management is responsible by garbage collection: do not give programmers explicitly release the ability of the object. Java does not specify the garbage collection algorithm for use, and a variety of algorithms can be used according to the needs of the system.

The Java method area is similar to the compiled code in the traditional language or the text segment in the UNIX process. It saves method code (compiled Java code) and symbolic table. In the current Java implementation, the method code is not included in the garbage collection stack, but it is planned to be implemented in future versions. Each class file contains a Java class or a compiled code for a Java interface. It can be said that the class file is the execution code file of the Java language. In order to ensure the platform independence of the class file, the format of the class file in the Java virtual machine specification has also been detailed. For details, please refer to Sun's Java virtual machine specification. The Java virtual machine register is used to save the running status of the machine, similar to some of the dedicated registers in the microprocessor. There are four registers of the Java virtual machine:

PC: Java Program Counter; Optop: Pointing to the Trip Top of Operation Tabs; Frame: Pointer to the execution environment of the current execution method; Vars: Pointer to the first variable of the first variable of the local variable area of ​​the currently executing method.

In the above architectural diagram, what we are saying is the first, namely the program counter, once each thread has its own program counter. When the thread executes the Java method, it contains the address of the instruction that the thread is being executed. However, if the thread is executed is a local method, the value of the program counter will not be defined.

The Java Virtual Machinery has three areas: local variable area, operating environmental area, operative area.

Partial variable area

Each Java method uses a local variable set for a fixed size. They are addressed in accordance with the word offset with the VARS register. Local variables are 32-bit. Long integer and double precision floating point occupies two local variables, but in accordance with the index of the first partial variable. (For example, a local variable with index N, if it is a double precision floating point number, then it actually occupies the storage space represented by index N and N 1) The virtual machine specification does not require 64 bits in local variables. The value is 64-bit. The virtual machine provides instructions that load the value in the local variable to the operator stack, and also provides instructions in writing a value in the operator stack.

Operating environment area

The information contained in the operating environment is used for dynamic links, normal methods return and exception capture.

Dynamic link

The operating environment includes a pointer to the interpreter symbol table to the current class and the current method for supporting the dynamic link of the method code. The Class file code of the method uses symbols when referenced to the method to be called and the variable to be accessed. The dynamic link translates the method of the symbol to the actual method call, load the necessary classes to explain the symbols that have not been defined and the variable access is translated into an offset address corresponding to the storage structure of these variables. Dynamic linking methods and variables make changes in other classes used in the method not affect the code of this program.

Normal method returns

If the current method is ended normally, the call to the call will get a return value when an returned command with the correct type is executed. The execution environment is used to recover the caller's register in the case of normal return, and add an appropriate value to skip the executed method call command, then continue execution in the caller's execution environment. Go down.

Abnormal capture

Abnormal situation is called Error or Exception in Java, which is the subclass of the Throwable class. The reason for the program is: 1 Dynamic link error, if the required Class file cannot be found. 2 Running is wrong, such as reference to an empty pointer. The program uses the throw statement.

When an exception occurs, Java virtual machine takes the following measures:

Check the catch clause of the CATCH in the current method. Each Catch clause includes its valid instruction range, which can handle an exception type, and processing an exception code block address. The Catch clause that matches the abnormality should comply with the following conditions: the abnormal instructions are within its instruction range, the abnormality type that occurs is a subtype of its ability to process an exception type. If a matching catch clause is found, then the system shifts at the specified exception handling block; if the exception processing block is not found, repeat the process of matching the Catch clause, until all nested Catch clauses of the current method Have been checked. Since the virtual machine proceeds from the first matching catch clause, the order in the CatCH sub-sentence table is important. Because the Java code is structured, there can always be arranged in a table in a table, and the value of any possible program counter can be used to find the appropriate exception handling in the order of linearity. Block to process an abnormality that occurs at the program counter value. If a matching catch clause is not found, the current method gets a "uncharacted exception" result and returns to the caller of the current method, as an exception just happens in its caller. If the corresponding exception processing block is still found in the caller, then this error will be spread. If the error is propagated to the top, the system will call a default exception handling block. Operation count

The machine instruction only takes the operand from the operator count, and the machine is operated, and the result is returned to the stack. The reason for selecting a stack structure is that the behavior of the virtual machine can also be efficiently simulated on only a small number of registers or non-universal registers (such as Intel486). The operator stack is 32-bit. It is used to transfer parameters to the method and receive the result from the method, also used to support the parameters of the operation, and save the results of the operation. For example, the IADD instruction adds two integers. The two integers that are added should be two words in the top of the operand. These two words are pressing the stack by the previous instruction. These two integers will pop up from the stack, add, press the result back into the operator stack.

Each raw data type has a special instruction to perform a necessary operation. Each operand requires a storage location in the stack, in addition to the long and double types, they need two locations. The operand can only be operated for operators whose types. For example, it is illegal if it is pressed into the number of two int types, if they are used as a number of LONG types. In the virtual machine implementation of Sun, this limit is enforced by the bytecode verifier. However, there is a small number of operations (operators DUPE and SWAP), which are not considered when operating the runtime data area.

The local square stack, when a thread calls the local method, it is no longer subject to the constraints of the virtual machine on the structure and security restrictions, which can access the virtual machine's runtime data area, or use local processors and any types of Stack. For example, the local stack is a stack of a C language, then when the C program is called C function, the parameters of the function are pressed into the stack in some order, and the result is returned to the call function. When implementing a Java virtual machine, the local method interface uses the C language model stack, then its local square stack is completely the same as the Stack of C language.

3 Java virtual machine running procedure

The above sections of the virtual machine are more detailed, and the following is analyzed by a specific example.

The virtual machine is activated by calling a specified class MAIN, passes to the main string array parameter, allowing the specified class to be loaded, and links the other types used in this class and initializes them. For example, for the program:

Class HelloApp

{

Public static void main (string [] args)

{

System.out.println ("Hello World!");

For (int i = 0; i

{

System.out.println (Args [i]);

}

}

}

Type in command line mode: Java HelloApp Run Virtual Machine

The Java virtual machine will be launched by calling the HelloApp method, passed to the MAIN a array containing three strings "run", "virtual", "machine". Now we will scream the steps that the virtual machine may take when performing HelloApp. Starting to try to execute the main method of HelloApp, it is found that the class is not loaded, that is, the virtual machine currently does not contain the binary representative of the class, so the virtual machine uses ClassLoader to try to find such a binary representative. If this process fails, an exception is thrown. After the class is loaded, the class HelloApp must be linked to other types before the main method is called and then initialized. The link contains three phases: inspection, preparation, and resolution. Check the symbols and semantics of the loaded primary class, ready to create a static domain of the class or interface, and initialize these domains to standard default values, resolve the symbol reference to the primary class or interface, in this step It is optional. The initialization of the class is the execution of the static initialization function and the initialization structure of the static domain declared. A class that has its parent class must be initialized before initialization. The whole process is as follows:

Figure 4: Operation of the virtual machine

4 Conclusion

This paper is intended to analyze the mechanism of the Java virtual machine by detailed analysis of the operation of the virtual machine when the JVM is executed and the operation of the virtual machine is executed.

转载请注明原文地址:https://www.9cbs.com/read-128367.html

New Post(0)