JVM virtual machine works



A class loader

First look at the execution of java programs.

                         

From this diagram it is easy to understand java program works in general. First, you write java code, save them to your hard disk. Then you enter on the command line

  1. javac YourClassName.java  
javac YourClassName.java

At this point, your java code is compiled into bytecode (.class). If you are in the Eclipse IDE or other development tools, you save the code, development tools have to help you complete the above compilation work, so you can see the class files in the corresponding directory. At this point the class file is still stored in the hard disk, so when you run the command line

  1. java YourClassName  
java YourClassName

To complete the work the red box above. JRE to loader reads class files from the hard disk, loaded into the system memory area allocated to the JVM - Run data area (Runtime Data Areas) execution engine then interpreted or compiled class files, the CPU into a specific machine code converted , CPU machine code execution, thus completing the whole process.


Then we focus on what class loader was exactly why? How is work?

First, a look at some of the features to the loader, a little abstract, but always helpful.


"" Hierarchy

Class loader are organized into a hierarchy of relationship, that is, the relationship between father and son. Which, Bootstrap class loader is the father of all. As shown below:

        

--Bootstrap class loader:

When running the java virtual machine, the class loader is created, it is loaded with some basic java API, including the Object class. Note that, the class loader is not written in java, but with C / C ++ to write.

--Extension class loader:

This loader loads the number of classes to expand beyond the basic API, including some related to the safety performance of the class. (Current understanding was not very deep, can only say that in general terms, to be detailed at a later date)

--System Class Loader:

It loads the application classes, which is configured in your classpath class.

--User-Defined Class Loader:

This is by expanding developer defined custom ClassLoader class loader, load some programmer-defined classes.


"" Delegate mode (Delegation Mode)

A closer look at the hierarchy above, when the JVM loads a class, the lower the loader will be entrusted the task to the one class loader, load on the floor to check whether it has been loaded namespace the class, if you have loaded, use this class directly. If not loaded, continue up until the top of the commission. After the inspection is over, carried out in the reverse order to load, if the Bootstrap loader can not find this class, the commission down until you find the class file. For a particular class loader, one Java class can only be loaded once, that is to say in the Java virtual machine, class is full identity (classLoader, package, className). Ray may be loaded a different class loader.


Give a concrete example to illustrate, join me now have their own definition of a class MyClass needs to be loaded, if not specified, the general cross-App (System) loaded. After receiving the mission, System check whether their library has this kind, found no later entrusted to the Extension, Extension perform the same inspection found that did not continue up the commission, the topmost Boots Curry did not find themselves, so according to its path (Java core class libraries such as java.lang) attempts to load, did not find this MaClass class, so he had to (people like you, give you complete, you can do nothing, I had it over to someone else) down to delegate Extension, Extension into their path (JAVA_HOME / jre / lib / ext) is looking for, or not found, continue down, then System loader to the classpath path looking for, find, and then loaded into the Java virtual machine.

Now suppose we will put this class JAVA_HOME / jre / lib / ext this path to go (equivalent to the Extension loader loads), according to the same rules, and finally loaded by the Extension class MyClass loader, see, harmonization of class is loaded twice into the JVM, but each time performed by different ClassLoader.


"" Visibility restrictions

The lower the loader can be seen in the upper class loader, not vice versa, that is to say only delegate from bottom to top.


"" Do not allow unloading category

Class loader can load a class, but it can not unload a class. But the class loader can be deleted or created.


After class when loaded, JVM continue with other work in the following diagram:


A block diagram of various steps briefly as follows:

Loading: class loading articles described earlier, the loaded Class file system memory to the JVM (operational data area)

Verifying: checking whether the loaded class files and Java Virtual Machine specification specification.

Preparing: the memory for the required allocation class, determining a required data structure the class attributes and methods. (Prepare a data structure that assigns the memory required by classes and indicates the fields, methods, and interfaces defined in the class.)

Resolving: the class constant pool symbolic references are changed to direct reference. (Not quite understand)

Initialing: local variables initialized class assignment for the static fields, while performing static initialization block.


So, Class Loader loaded class, what can be done to work it?

To understand the details of which must have to explain in detail the operational data area.


Second, the operational data area

Runtime Data Areas: When running a JVM example, the system assigned to it a memory area (the size of this memory area can be set), the memory area is managed by the JVM themselves. Separated from one memory to store a number of operating data, such as an object created to pass parameters to methods, local variables, return values, etc. Points out this piece is called operational data area. Operating the data area may be divided into 6 chunks: Java stacks, program counter register (PC register), native method stacks (Native Method Stack), Java heap area method, constant pool operation (Runtime Constant Pool). The constant pool running this method should belong to the district, but because of its importance, JVM specification to be independent explanation. Among them, in front of the regional 3 (PC register, Java stack, native method stacks) each thread is independently owned, the latter three are all threads in the entire JVM instance common. The six chunks as shown below:


"PC counter:

Each thread has a PC counter, when the thread starts (start), PC counter is created, the counter is stored bytecode instructions (JVM instruction) it is currently being executed address.

"Java Stack:

The same, Java stack for each thread is individually owned, created when the thread starts. The stack frame is stored a series stack (Stack Frame), JVM only press-fitting (the Push) and pop (Pop) stack frame both. Whenever you call a method, JVM go out into the stack pressed into a stack frame, pop the stack frame when the method ends return. If an exception occurs when the method execution, you can call printStackTrace other methods to view the situation stack. A schematic view of the stack as follows:


OK. Now let's look at the details of each stack frame are placed something. Readily apparent from the schematic, each stack frame comprises three parts: an array of local variables, an operand stack, the method belongs to the class constant pool reference.

"Local (local) array of variables:

Topical (local) variable array , the order starting from 0 to store the object reference associated method, parameters passed to the method, the local variables. for example:

  1. public void doSomething(int a, double b, Object o) {  
  2. ...  
  3. }  
public void doSomething(int a, double b, Object o) {
...
}

SUMMARY stack frame of this method, local variables are stored:

  1. 0this  
  2. 1: a  
  3. 2,3:b  
  4. 4:0  
0: this
1: a
2,3:b
4:0

Look carefully which type double b requires two consecutive index. Value when the value of 2 is taken out of the index. For a static method, the array of storage of this reference is not 0, but directly stored in the transmission parameters.

"Operand stack:

Operand stack store some method for performing the intermediate variables, JVM press-pop or variables when performing the method. In fact, the operand stack is where the real work of the method, when performing the method, local variables and operand stack array data exchange method according to the definition. For example, the following code is executed, where the operand stack is as follows:

  1. int a = 90;  
  2. int b = 10;  
  3. int c = a + b;  
int a = 90;
int b = 10;
int c = a + b;


Note that in this figure, the point operand stack 100 is positioned above the upper side, so that the first press-fitting. As can be seen, the operand stack is actually a temporary data storage area to store some intermediate variables, the method is over, there will be no operand stack it.

"Stack frame data reference:

In addition to an array of local variables and operand stack, the stack frame is also a need to reference constant pool. When the data needed to perform the JVM constant pool, access is through the constant pool references. Data stack frame also responsible approach and abnormal returns. If the return by return, then the frame is popped from the process stack Java stack. If the method has a return value, the return value will be pushed into the process the method call of the operand stack. Further, the data stored in the area method may further exception table references. The following examples are presented to illustrate:

  1. class Example3C{  
  2.     public static void addAndPrint(){  
  3.         double result = addTwoTypes(1,88.88);  
  4.         System.out.println(result);  
  5.     }  
  6.     public static double addTwoTypes(int i, double d){  
  7.     return i+d;  
  8.     }  
  9.   
  10. }  
class Example3C{
    public static void addAndPrint(){
        double result = addTwoTypes(1,88.88);
        System.out.println(result);
    }
    public static double addTwoTypes(int i, double d){
    return i+d;
    }

}

When the code is executed, Java stacks as shown below:


Take time to carefully study the map. As it should be noted that, over the bottom of the stack, the first stack frame addAndPrint charge art methods, then pressed into the stack frame addTwoTypes method. Rightmost characters on the map there is an error, the results should be stored in addTwoTypes operand stack addAndPrint in.

"" Native method stacks

When the program calls the local method (such as C or C ++ code) through the JNI (Java Native Interface), established according to the language type of the corresponding stack of native methods.

"" Method area

The method area is a JVM instance shared by all threads, when starting a JVM instance, the method area is created. It is for storing operation of a discharge constant pool, field and information about the bytecode methods, static variables, classes, and methods. Different JVM implementations area at the time of implementation will be different. Oracle's HotSpot called permanent area (Permanent Area) or permanently substituting (Permanent Generation).

"" Run constant pool

This area and the constant storage class interface, in addition, it holds all references to methods and fields. When a method or field is referenced, JVM on to find the actual address methods and fields in memory of those references by running constant pool.

"" Heap (Heap)

Heap is stored in an object or instance of the program created. This area great influence on the performance of the JVM. It is this area of ​​memory garbage collection process.

Therefore, the class loader loads is actually based on Class compiled files, loading the java bytecode JVM memory, and complete operational data in initialization, execution engine for execution.


Third, the execution engine (Execution Engine)

After the byte code class loader loaded into memory, the execution engine of the Java bytecode instructions Hopefully, Java byte code is read. The problem is that now the java bytecode machine is not read, it must also find a way to convert bytecode into machine code platform dependent. This process can be executed by an interpreter, you can have time compiler (JIT Compiler) to complete.





    
Published 11 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/zhengyin_tmac/article/details/52370082