Java technology and Java virtual machines

Source: Internet
Author: User
Tags vars

Java technology and Java virtual machines

Speaking of Java, the first thing people think of is the Java programming language, but in fact, Java is a technology, it is composed of four aspects:Java programming language ,Java class file format ,Java Virtual machine and Java Application Interface (Java API). Their relationship is as follows:


Figure 1 Java four aspects of the relationship


The runtime environment represents the Java platform, and the developer writes the Java code (. java file) and compiles it into a bytecode (. class file). The last bytecode is loaded into memory, and once the bytecode enters the virtual machine, it is interpreted by the interpreter to execute, or it is selected by the real-time code generator to be converted into machine code execution. It can also be seen that the Java platform is built by Java virtual machines and Java application interfaces, and the Java language is the gateway to this platform, and programs written and compiled in the Java language can run on this platform. The structure of this platform is as follows:


(Click to view the original)


In the Java platform architecture, it can be seen that the Java Virtual Machine (JVM) in the core location, is the program and the underlying operating system and hardware -independent key. Underneath it is the porting interface , which consists of two parts: the adapter and the Java operating system , where the platform-dependent part is called the adapter, and the JVM is implemented on the specific platform and operating system through the porting interface; Above are Java's basic class libraries and extension class libraries and their APIs, applications written using the Java API (application) and applets (Java applets) can be run on any Java platform without regard to the underlying platform, because there are Java virtual machines ( JVM) enables the separation of the program from the operating system, thus achieving the platform independence of Java.

So what exactly is a Java Virtual machine (JVM)? Usually when we talk about the JVM, we may mean:

A more abstract description of the JVM specification;
Specific implementation of the JVM;
A JVM instance that is generated during a program run.

The abstract description of the JVM specification is a collection of concepts that have been described in detail in the book "Java Virtual Machine Specification" (Java VM specification); The specific implementation of the JVM is either software, It is either a combination of software and hardware that has been implemented by many manufacturers and exists on multiple platforms; The task of running a Java program is assumed by the runtime instance of the JVM. The Java Virtual Machine (JVM) We are discussing in this article is primarily for the third case. It can be seen as an imaginary machine, in the actual computer through the software simulation to achieve, have their own imagination of the hardware, such as processors, stacks, registers, etc., as well as their own corresponding instruction system.

The JVM has a definite task in its life cycle, which is to run a Java program, so when the Java program starts, it generates an instance of the JVM, and the instance disappears when the program is finished running. Let's take a closer look at the JVM's architecture and its running process.

Architecture of the Java Virtual machine

As mentioned earlier, the JVM can be implemented by different vendors. Because the vendor's differences inevitably lead to some differences in the implementation of the JVM, the JVM can also implement cross-platform features, thanks to the architecture of the JVM design.

We know that the behavior of a JVM instance is not just about its own, but also about its subsystems, storage areas, data types, and directives, which describe an abstract internal architecture of the JVM that not only prescribes its internal architecture when implementing the JVM, but more importantly provides a way to Used to strictly define the external behavior when implemented. Each JVM has two mechanisms, one is to load a class (class or interface) with the appropriate name, called the class loading subsystem, and the other is responsible for executing the instructions contained in the loaded class or interface, called the Run engine. Each JVM also includes five parts of the method area, heap, Java stack, program counter, and local method stack, and the architecture diagram of these parts and the class loading mechanism together with the running engine mechanism is:


Figure 3 Architecture of the JVM


Each instance of the JVM has its own method domain and a heap, and all the threads running within the JVM share these areas, and when the virtual machine loads the class file, it parses the class information contained in the binary data and puts them in the method domain; The JVM places all the objects initialized by the program on the heap, and each thread is created with its own program counter and Java stack, where the value in the program counter points to the next instruction to be executed, and the Java stack of the thread is stored as the thread invokes the state of the Java method. The state of the local method call is stored on the local method stack, which relies on a specific implementation.

These sections are described separately below.

The execution engine is at the core of the JVM, and its behavior is determined by the instruction set in the Java Virtual Machine specification. Although for each instruction, the specification explains in detail what the implementation of the JVM should do when it executes the bytecode, but does little to make. The Java virtual machine supports approximately 248 byte codes. Each bytecode performs a basic CPU operation, such as adding an integer to the register, subroutine transfer, and so on. The Java instruction set is equivalent to the assembly language of the Java program.

Directives in the Java instruction set contain a single-byte operator that specifies the action to perform, as well as 0 or more operands, providing the parameters or data required for the operation. Many instructions do not have operands and consist of only one single-byte operator.

The inner loop of the virtual machine is executed as follows:

do{
Take an operator byte;
Performs an action based on the value of the operator;
}while (program not finished)

Because of the simplicity of the instruction system, the process of virtual machine execution is very simple, which helps to improve the efficiency of execution. The number and size of operands in an instruction is determined by the operator. If the operand is larger than one byte, the order in which it is stored is the high byte priority. For example, a 16-bit parameter holds two bytes, and its value is:

The first byte *256+ a second byte bytecode.

The instruction stream is generally just byte-aligned. Directives Tableswitch and lookup are exceptions, and within these two directives the mandatory 4-byte boundary alignment is required.

For a local method interface, implementing the JVM does not require it to be supported, or even completely. Sun's implementation of the Java Local interface (JNI) is a portability concern, and of course we can design other local interfaces to replace Sun's JNI. But these designs and implementations are more complex things, and you need to make sure that the garbage collector does not release objects that are being called by local methods.

The Java heap is a run-time data area where instances (objects) of a class allocate space, and its management is handled by garbage collection: it does not give programmers the ability to explicitly release objects. Java does not specify a garbage collection algorithm to use, and can use a variety of algorithms depending on the needs of the system.

The Java method area is similar to the post-compilation code in a traditional language or a body segment in a UNIX process. It saves the method code (compiled Java code) and the symbol table. In the current Java implementation, the method code is not included in the garbage collection heap, but is scheduled to be implemented in a future release. Each class file contains the compiled code for a Java class or a Java interface. It can be said that the class file is the Java language Execution code file. In order to guarantee the platform independence of the class file, the format of the class file in the Java Virtual Machine specification is also described in detail. Refer to Sun's Java Virtual Machine specification for specific details.

The registers of the Java Virtual machine are used to hold the machine running in a similar form to some specialized registers in the microprocessor. There are four types of registers for Java virtual machines:

Pc:java program counter;
Optop: Pointer to the top of the operand stack;
Frame: A pointer to the execution environment of the current execution method;
VARs: A pointer to the first variable in the local variable area of the current execution method.

In the above architecture diagram, we are talking about the first, the program counter, each thread has its own program counter once it is created. When a thread executes a Java method, it contains the address of the instruction that the thread is executing. However, if the thread executes a local method, the value of the program counter is not defined.

The Java Virtual machine stack has three regions: local variable area, running environment area, operand area.

Local variable Area

Each Java method uses a fixed-size set of local variables. They are addressed by the word offset to the VARs register. Local variables are 32-bit. Long integers and double-precision floating-point numbers occupy two of the space of local variables, but are addressed by the index of the first local variable. (for example, a local variable with index n, if it is a double-precision floating-point number, it actually occupies the storage space represented by index N and n+1) The virtual machine specification does not require that the 64-bit value in the local variable be 64-bit aligned. The virtual machine provides instructions for loading the values in the local variable into the operand stack, as well as instructions for writing the values in the operand stack to local variables.

Operating Environment Area

The information contained in the run environment is used for dynamic linking, normal method returns, and exception snapping.

Dynamic links

The run environment includes pointers to the interpreter symbol table that point to the current class and the current method, and is used to support dynamic linking of method code. The class file code of the method uses the symbol when referencing the method to be called and the variable to be accessed. Dynamic linking translates method calls in symbolic form into actual method calls, loads the necessary classes to interpret symbols that have not yet been defined, and translates the variable access into an offset address corresponding to the storage structure at which these variables run. Dynamic linking methods and variables make changes to other classes used in the method do not affect the code of this program.

Normal method to return

If the current method ends normally, the method called will get a return value when a return instruction with the correct type is executed. The execution environment is used to recover the caller's register in the case of normal return, and the caller's program counter is added with an appropriate value to skip the executed method invocation instruction and continue execution in the caller's execution environment.

Exception snapping

Exceptions are referred to as error (Error) or exception (exceptions) in Java, and are subclasses of the Throwable class, in which the reason for this is that the ① dynamic link is wrong, such as the required class file cannot be found. ② run-time error, such as a reference to a null pointer. The program uses a throw statement.

When an exception occurs, the Java Virtual machine takes the following actions:

Checks the catch clause table that is associated with the current method. Each catch clause contains its valid instruction range, the type of exception it can handle, and the address of the code block that handles the exception.

A catch clause that matches an exception should meet the following criteria: the instruction that caused the exception is within its scope, and the exception type that occurs is the subtype of the exception type it can handle. If a matching catch clause is found, the system is transferred to the specified exception-handling block, and if no exception-handling block is found, the process of finding a matching catch clause is repeated until all nested catch clauses of the current method are checked.

Because the virtual machine continues execution from the first matching catch clause, the order in the CATCH clause table is important. Because Java code is structured, you can always order all the exception handlers of a method into a table, and for any possible program counter value, you can find the appropriate exception handling block in a linear order to handle the exception that occurs under the program counter value.

If a matching catch clause is not found, the current method gets the result of an "not intercepted exception" and returns to the caller of the current method, as if the exception had just occurred in its caller. If the corresponding exception-handling block is still not found in the caller, the error is propagated. If the error is propagated to the topmost level, then the system invokes a default exception handling block.

Operand Stack Area

The machine instructions only take the operands from the operand stack, manipulate them, and return the results to the stack. The reason for choosing a stack structure is that the behavior of the virtual machine can be simulated efficiently on machines with only a few registers or non-universal registers, such as Intel486. The operand stack is 32 bits. It is used to pass parameters to the method and receive the result from the method, also to support the parameters of the operation, and to save the result of the operation. For example, the IADD directive 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 are ejected from the stack, added together, and the result is pressed back into the operand stack.

Each raw data type has special instructions to perform the necessary operations on them. Each operand requires a storage location in the stack, except for long and double types, which require two positions. The operand can only be manipulated by an operator that is applicable to its type. For example, it is illegal to press into a number of two int types if they are treated as a long type. In Sun's virtual machine implementation, this restriction is enforced by the bytecode validator. However, there are a few operations (operator dupe and swap) that are used to manipulate the runtime data area regardless of type.

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.

The running process of a Java virtual machine

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

The Java Virtual machine will be started by calling HelloApp's method main, passing to main an array containing three strings "run", "virtual", "machine". Now we outline the steps that the virtual machine may take when executing the HelloApp.

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:


(Click to view the original)
Figure 4: Virtual machine running Process

  Conclusion

This paper is intended to analyze the mechanism of Java Virtual Machine by deeply studying the architecture of JVM and analyzing the running process of virtual machine during the execution of Java program.

Java technology and Java virtual machines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.