jvm prepared from the java program executed by the link to the structure

jvm prepared from the java program executed by the link to the structure


1.java java front end compiler is responsible for compiling the source code into bytecodes -> compiler front end

2.java virtual machine is responsible for loading bytecode compiled into the interior -> the step of loading the operating region and class java

Class load: loaded, link initialization

Load : class loading stage is the responsibility of the class loader to read such binary byte flows inside the JVM according to a fully qualified name of the class , method and stored in the run-time memory area in the zone, and then convert it into a the corresponding to the target type java.lang.Class object instance, the Class object will in the future as the access method entry area various data class.
Link : will be loaded into the JVM in the class of binary data byte stream of information into the JVM operating state , the connection phase by the authentication, and parsing prepare three stages configuration. The main task of the validation phase is to verify whether the data type JVM specification. The main task of the preparation phase is to allocate memory space for the class of all static variables, and set the initial value. Parsing stage all the symbolic constant pool references all converted into a direct reference. But JVM specification does not provide for the implementation of resolution phase according to a certain order, so the resolution phase can then perform until after initialization.
Initialization : the JVM will be in a class all static code or code blocks identified keywords all performed again.

Class loader


Bootstrap Class Loader also become a boot loader is responsible for loading all types of "JAVA_HOME / lib" directory
All types ExternalClassLoader responsible for loading the "JAVA_HOME / lib / ext" extension directory
All types appClassLoader responsible for loading the next ClassPath

Parent delegation model

Parent delegation model (Parent Delegation Model):

Loading class takes parents entrust mechanism that can better ensure the safety of the Java platform.
This model requires addition top Bootstrap class loader boot class loader, the rest of the class loader should have its own 父类加载器. Child and parent class loader class loader 不是以继承(Inheritance)的关系is achieved, but by 组合(Composition)关系multiplexed codes with parent loader. Each class loader has its own namespace (of the loader and all the parent class loader loaded classes in the same namespace, class name does not appear complete (including package of) the same the two classes; in a different namespace, there may be a full class name (including package of) two of the same class)

Parents can delegate process model is as follows:

1. Does the current ClassLoader first queries from his already loaded class such has been loaded, if already loaded directly back to the original already loaded class.

每个类加载器都有自己的加载缓存,当一个类被加载了以后就会放入缓存, 等下次加载的时候就可以直接返回了。 

2. Current classLoader not found in the cache loaded class, commissioned by the parent class loader to load, the parent class loader using the same strategy, first check its cache, then delegate the parent class of the parent class to load, has been to ClassLoader on Bootstrap.
3. when all the parent class loader are not loaded, and then loaded by the current class loader, and placed in its own cache, so next time there is a load request return directly.

The benefits of using this model to organize the relationship between the class loader:
mainly to 安全性avoid user-written Java class dynamically replace some core classes, such as String, but also to avoid 重复加载, because the JVM to distinguish between different classes, not only It is just loaded two different classes based on the class name, the same class file different ClassLoader, if mutual transformation, then will throw java.lang.ClassCaseException.

Custom class loader

Only need to inherit the abstract class ClassLoader and rewrite findClass () method can be. Converting the read incoming binary array into an instance of class Class used in the defineClass () method.

3. Finally interpreted / compiled into machine instructions executed on the corresponding platform -> jvm execution engine and the garbage collection GC

 Java bytecode is executed in two ways:
  1. in-time compiler manner: first interpreter bytecode compiler into machine code, and then executing the machine code. 
  2. interpretive execution mode: by an interpreter interprets and executes each piece of code to perform all operations of Java bytecode programs. 

jdk and openjdk hotspot vm is in default comes with a virtual machine. Within the hotspot vm, in-time compiler and interpreter co-exist. This is because when the virtual machine is powered on, the interpreter can do their work first, without waiting for the compiler to compile all complete before execution, so can save a lot of unnecessary compilation time. Over and program run time, the compiler to function progressively, hot detection function according to the value (frequently called methods) byte code compiled to native machine instructions in exchange for a higher efficiency of program execution.



Published 159 original articles · won praise 75 · views 190 000 +

Guess you like

Origin blog.csdn.net/xuehuagongzi000/article/details/70054576