Go Java detailed JVM working principle and process

Source: Internet
Author: User

As a Java user, it is also necessary to master the architecture of the JVM.
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:

The runtime environment represents the Java platform, the developer writes Java code (. java file), compiles it into a bytecode (. class file), and then the bytecode is loaded into memory, and once the bytecode enters the virtual machine, it is interpreted and executed by the interpreter. Or the real-time code generator has the option of converting to machine code execution.

The Java platform is built by Java virtual machines and Java application interfaces, and the Java language is the gateway into this platform, and programs written and compiled in the Java language can run on this platform. The structure of this platform is as follows:

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 via the porting interface, and above the JVM is the basic Java class Library and the Extension class library and their APIs. Applications written using the Java API (application) and applets (Java applets) can run on any Java platform without regard to the underlying platform, because Java virtual machines (JVMS) Implement the separation of the program from the operating system, enabling Java Platform-independent nature.

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.

1. Architecture of Java Virtual machines

• There are two types of mechanisms for each JVM:

① class loading subsystem: Loading a class or interface with a suitable name

② Execution Engine: responsible for executing instructions contained in a loaded class or interface

• Each JVM contains :

Method area, Java heap, Java stack, local method stack, instruction counter, and other implied registers


For the study of the JVM, it seems to me that so many parts are most important:

The entire process of compiling and executing Java code

JVM memory management and garbage collection mechanism

These sections are explained below:

2. The entire process of compiling and executing Java code

As mentioned earlier, the whole process of compiling and executing Java code is probably: the developer writes Java code (. java file), compiles it into a bytecode (. class file), and then the bytecode is loaded into memory, and once the bytecode enters the virtual machine, it is interpreted by the interpreter, Or the real-time code generator has the option of converting to machine code execution.

(1) Java code compilation is done by the Java source compiler, which is the Java code to the JVM bytecode (. class file) process. The flowchart is as follows:

(2) Java bytecode execution is done by the JVM execution engine, and the flowchart is as follows:

The entire process of compiling and executing Java code consists of the following three important mechanisms:

· Java source Code compilation mechanism

• Class loading mechanism

• Class execution mechanism

(1) Java source Code compilation mechanism

Java source code compilation consists of the following three processes:

① analysis and input to symbol table

② annotation Processing

③ semantic analysis and generation of class files

The flowchart is as follows:

The resulting class file is made up of the following sections:

① structure Information: Includes the class file format version number and the parts of the number and size of information

② metadata: Information that corresponds to declarations and constants in Java source code. Contains class/inherited superclass/implemented interface declaration information, domain and method declaration information, and constant pool

③ method Information: corresponding to the Java source code in the statement and expression corresponding information. Includes byte code, exception Processor table, evaluation stack and local variable size, type record of evaluation stack, debug symbol information

(2) class loading mechanism
The class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of the classes can be described as follows:

①bootstrap ClassLoader

Responsible for loading all classes in Jre/lib/rt.jar in $java_home, implemented by C + +, not classloader subclasses

②extension ClassLoader

Some jar packages that are responsible for loading the extensions in the JAVA platform, including the jar packages in $java_home Jre/lib/*.jar or-djava.ext.dirs specified directories

③app ClassLoader

Responsible for documenting the jar packages specified in the Classpath and the class in the directory

④custom ClassLoader

The ClassLoader that the application customizes according to its own needs, such as Tomcat and JBoss, will be implemented according to the Java EE specification itself ClassLoader

The load process checks to see if the class is loaded, the check order is bottom-up, and the custom ClassLoader to bootstrap ClassLoader-by-layer check, as long as a certain classloader is loaded as if the class has been loaded. Ensure that only all classloader of this class are loaded once. The order of loading is top-down, that is, the upper layer tries to load the class one at a level.

(3) class execution mechanism

The JVM is a stack-based virtual machine. The JVM allocates a stack for each newly created thread. That is, for a Java program, it runs through the operation of the stack. The stack holds the state of the thread in frames. The JVM operates on the stack in only two ways: stack and stack operations in frames.

The JVM executes the class bytecode, and after the thread is created, the program counter (PC) and stack (stack) are generated, and the program counter holds the offset of the next instruction to be executed in the method, the stack frame is stored, and each stack frame corresponds to each call of each method. And the stack frame has the local variable area and the operand stack two parts, the local variable area is used to store the local variable and the parameter in the method, the operand stack is used to hold the intermediate result produced in the method execution process. The structure of the stack is as follows:

3. JVM memory management and garbage collection mechanism

The JVM memory structure is divided into: Method area, stack memory (stack), heap memory (heap), local method stack (JNI call in Java), and the structure diagram is as follows:

(1) heap in memory (heap)

All the memory of objects created by new is allocated in the heap, and its size can be controlled by-XMX and-XMS.
The operating system has a linked list that records the free memory address, and when the system receives a request for a program, it iterates through the list, finds the heap node where the first space is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program, and for most systems, The size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code can properly free up the memory space. However, because the size of the found heap node does not necessarily equal the size of the request, the system will automatically re-put the extra portion into the idle list. At this point, the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use. In addition, under Windows, the best way is to use VirtualAlloc to allocate memory, it is not in the heap, nor in the stack, but directly in the process of the address space to retain a piece of memory, although this method is most inconvenient, but fast, but also the most flexible. Heap memory is a data structure that extends to high addresses and is a discontinuous area of memory. Because the system is stored with a linked list of free memory address, the nature is not contiguous, and the link table traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large .

(2) Stack memory (stack)

Under Windows, the stack is the data structure that extends to the low address, which is a contiguous area of memory . This sentence means that the top of the stack address and the maximum capacity of the stack is the system pre-defined, under Windows, the size of the stack is fixed (is a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small . As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow. Automatically assigned by the system, faster. But programmers can't control it.

Heap memory and stack memory need to be explained:

The underlying data type is directly allocated in the stack space, and the form parameters of the method are allocated directly in the stack space when the method call is completed and reclaimed from the stack space. The reference data type needs to be created with new, which allocates an address space in the stack space and allocates the class variables of the object in the heap space. The reference parameter of the method, which allocates an address space in the stack space, and points to the object area of the heap space, which is reclaimed from the stack space when the method call is complete. When the local variable new comes out, allocates space in the stack space and heap space, when the local variable life cycle ends, the stack space is immediately reclaimed, and the heap space area waits for GC to reclaim. The literal parameter passed in the method invocation is first allocated in the stack space and retracted from the stack space after the method call is complete. string constants, static assignments in the data area, this is allocated in heap space. An array allocates both the array name in the stack space and the actual size of the array in the heap space.

Such as:

(3) Local method stack (JNI invocation in Java)

Used to support the execution of the native method, which stores the state of each native method call. 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.

(4) Method area

It saves the method code (compiled Java code) and the symbol table. Contains the class information to load, static variables, constants of the final type, properties, and method information. The JVM uses a durable generation (permanet Generation) to store the method area, and the minimum and maximum values can be specified by-xx:permsize and-xx:maxpermsize.

Garbage collection mechanism

The heap gathers all the objects created by the application, and the JVM has instructions such as new, NewArray, Anewarray, and Multianewarray, and there is no command to free up space to C + + Delete,free, and all releases of Java are by GC To do, the GC in addition to recycling memory, another important task is the compression of memory, this in other languages also have similar implementations, compared to C + + not only good, but also increased security, of course, she has drawbacks, such as the big problem of performance.

4. Examples of running 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:

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:

This article from: Http://blog.csdn.net/CSDN_980979768/article/details/47281037?locationNum=7&fps=1

Go Java detailed JVM working principle and process

Related Article

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.