Virtual machine class loading mechanism

Source: Internet
Author: User
Tags array definition field table sap netweaver

The virtual machine loads the data of the description class from the class file into the memory, verifies the data, transforms parsing and initializing, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine.

The life cycle of class loading includes: loading loading, validating verification, preparing preparation, Parsing resolution, Initializes the initializationwith using and unload unloading.

In addition to the parsing phase, the sequence of several other stages is fixed. The parsing phase can be started in some cases after the initialization phase, which is to support runtime bindings for the Java language (dynamic binding/late binding)

The virtual machine specification strictly stipulates that there are only four cases where the class must be initialized (loaded, validated, ready to be automatically started before)

    1. When the New,getstatic,putstatic,invokestatic 4 bytecode directive is encountered, it is initialized first if the class is not initialized. The common scenario for these 4 bytecode is when instantiating an object with the New keyword , reading or setting a static field (except for a static field that has been put into the constant pool at compile time), and The time to invoke a static method of a class .
    2. When the reflection is tuned
    3. When a class is initialized, the parent class initializes first if its parent class is not initialized .
    4. When the virtual machine starts, the user needs to specify a main class to execute, and the virtual opportunity initializes the main class first .
These 4 cases are referred to as the Active referencing, other conditions are called Passive References。 For access to a static field, only the class that directly defines the field is initialized, so the child class refers to the static field defined in the parent class. only triggers initialization of the parent class without triggering the initialization of the subclass。 But for a hotspot, the loading of the subclass is triggeredreferencing a class through an array definition does not trigger initialization of this classconstants are stored in the constant pool of the calling class at compile time and are not inherently referenced directly to the class that defines the constants, so initialization of classes that define constants is not triggered。 interface loading and class loading are slightly different, interfaces cannot have static code snippets, but the <clinit> () class constructor is generated in the interface to initialize the member variables defined in the interface. when an interface is initialized, its parent class is not required to initialize thethe process of class loading LoadingDuring the load phase, the virtual machine needs to complete the following three things:
    1. A binary byte stream that defines this class is obtained through the fully qualified name of a class.
    2. Convert the static storage structure represented by this byte stream into the run-time data structure of the method area
    3. Generate a Java.lang.Class object in the Java heap that represents various types of access to the data as a method area
In fact, these three qualifiers are not very strict, such as the first one, does not explicitly indicate where to get the binary stream through the fully qualified name, thus there are many different implementations:
    • Read in the ZIP package (Jar,ear,war)
    • Getting (applets) from the network
    • Run-time compute generation, this scenario uses the most is the dynamic agent technology, in the Java.lang.reflect.Proxy, is uses the Proxygenerator.generateproxyclass to for the specific interface Shanghai $ Binary stream of proxy classes for proxies
    • Generated by other files (JSP)
    • Read from the database, some middleware servers (SAP NETWEAVER)
When the load phase is complete, the binary stream outside the virtual machine is in the format required by the virtual machine stored in the method area, the data storage format in the method area is defined by the virtual machine implementation itself. A Java.lang.Class class object is then instantiated in the Java heap, which acts as the external interface for the program to access these types of data in the method area. The loading phase and part of the connection phase are interleaved, the loading phase is not complete and the connection phase may have startedValidationValidation is the first step in the connection phase, and the purpose of this phase is to to ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine, and it does not compromise the security of the virtual machine itself. some things that can be controlled at the compiler level (such as hyper-boundary access to an array, cross-type object conversions exist, the compiler refuses to work) can be cracked by modifying the class file directly, which is why the validation phase exists。 According to the virtual machine specification, if you verify that the input byte stream does not conform to the storage format of the class file, it throws an Java.lang.VerifyError exception or its subclass exception. The verification process is roughly divided into 4 phases: file Format Verificationmeta-data validationbyte code verificationAnd Symbol Reference ValidationFile format verification: such as whether to start with the magic number, major and minor version number is within the scope of the virtual machine can be processed, the constant pool is not supported type, and so on. after this phase of validation, the byte stream will be stored in the memory's method areaSo The following three verification phases are all based on the storage structure of the method area。 Metadata validation: The information that is described by the byte code is Semantic AnalysisTo ensure that the information it describes conforms to the requirements of the Java language Specification, which may include a validation point that includes whether the class has a parent class, whether the parent class integrates a class that does not allow inheritance, or whether the field in the class and the parent class are inconsistent if the class is not implementing all of the methods required in its parent class or interface Bytecode verification: one of the most complex interpretations that, the main work is perform data flow and control flow analysis。 At this stage, the method body of the class is verified and analyzed to ensure that the method does not make the behavior that harms the JVM security at runtime, for example: to ensure that the data type and instruction code sequence of the operand stack at any time can work together to ensure that the jump instruction does not jump to the bytecode instruction outside the method body. Ensure that the type conversion in the method body is valid ... This verification does not guarantee a certain safety ( stop problem, through the program to verify the program logic is not absolutely accurate1.6 Adding the Stackmaptable function optimizes this phase and improves speed, but this stackmaptable can also be tampered with, and it is possible to turn this option off with the start parameter. Symbol Reference Validation: This stage occurs when a virtual machine converts a symbolic reference to a direct reference. This conversion action will occur during the third phase of the connection----the parsing phase。 Can be seen as a validation of the matching of information other than the class itself. For example: the fully qualified name of the symbol reference in the string description can find the corresponding class, whether there is the method and the field described .... If the symbol cannot be verified, a subclass of the Java.lang.IncompatibleClassChangeError exception will be thrown, such as Java.lang.IllegalAccessError, Java.lang.nosuchfielderror,java.lang.nosuchmethoderror You can use the startup parameters to turn off most of the class validation measures to shorten the load time of the virtual machine classPrepareThe prep phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, which are allocated in the method area. This time inside The storage allocation includes only class variables (static variables), not instance variables, and instance variables are allocated in the Java heap along with the object when the object is instantiated。 This is followed by the initial value, " usually"is a 0 value of the data type ( The initial value of the definition is then generated during the initialization phase)。 If the variable is final decorated, Constantvalue is generated at compile time so that the initial value is set directly at the preparation stageparsingThe parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference. The symbol reference in the CLASS file appears as a symbol reference for constants of type Constant_class_info, Constant_fieldref_into, Constant_methodref_info, and so on: (symbolic References) symbol references describe the referenced target in a set of symbols, which can be any form of literal, and the referenced target is not necessarily loaded into memory, regardless of the virtual machine memory layout. Direct reference: Direct reference can be a pointer directly to the target, a relative offset, or a handle that can be References to the target indirectly. is related to the virtual machine memory layout. The   virtual machine specification does not specify the time at which the parsing phase occurs, only the execution of Anewarray,checkcast,getfield,getstatic,instanceof,invokeinterface is required, Invokespecial,invokestatic,invokevirtual,multianewarray,new,putfield,putstatic These 13 operation symbols refer to the bytecode Directive, The symbolic references they use are parsed first.   Multiple parse requests for the same symbol reference is a common thing. Some implementations are cached. Parsing actions are mainly for classes/interfaces, fields, class methods, interface methods, and four types of symbolic references. Constant_class_info,constant_fieldref_info,constant_methodref_info,constant_interfacemethodref_ corresponding to the constant pool, respectively Info four types.   Class and interface parsing: Assuming that the current code is in the Class D, if you want to parse an never-parsed symbol reference n into a direct reference to a class or interface C, the virtual machine needs the following three steps to complete the parsing process:
    1. If C is not an array type, the virtual machine will pass the fully qualified name representing N to the class loader of D to load the class C.
    2. If C is an array type, and the element type of the array is an object, it is handled as 1. if the element type is not an object, the virtual machine generates an Array object that represents this array dimension and element .
    3. If there is no exception to the above steps, C has become a valid class or interface in the virtual machine century, but before parsing is complete, symbolic reference validation is performed, confirming that C has access to D, and if not, throws a Java.lang.IllegalAccessError exception.
Field resolution: To parse a non-parsed field symbol reference, first the Constant_class_info symbol reference for the index in the Class_index item in the field table will be parsed, that is, the class or interface to which the field belongs, if the parse succeeds, That means that the class or interface to which this field belongs is represented by C, and the virtual machine specification requires that c be searched for subsequent fields in the following steps:
    1. If C itself contains this field, the direct reference to the field is returned, and the lookup ends
    2. Otherwise, if the interface is implemented in C, the interface and its parent interface will be searched recursively from the root of the integration relationship, and if so, the direct reference to the field is returned, and the lookup ends
    3. Otherwise, if C is not java.lang.Object, the parent class will be recursively searched according to the inheritance relationship, and if it matches, the return value is quoted, and the lookup ends
    4. Find failed, throw Java.lang.NoSuchFieldError exception.
itself--interface--parent class --FailureIf a reference is successfully returned, permission validation is performed, and the Java.lang.IllegalAccessError class method parsing is thrown without permission: The first step, like the field resolution, is to parse out the class method table first Class_ The symbol reference for the class or interface to which the method indexed in the index item belongs, and then follow these steps to search:
    1. The class method and interface method symbol reference the constant type definition is separate, if the Class_index index is found in the class method table C is an interface, that directly throws Java.lang.IncompatibleClassChangeError exception
    2. Find if there is a match in C, then end
    3. Otherwise, find a match in the C parent class
    4. Look in the C interface list and parent interface, if there is a match, C is abstract class, throw Java.lang.AbstractMethodError
    5. Throws Java.lang.NoSuchMethodError.
Return successful check permission validation, fail to throw Java.lang.IllegalAccessError interface method parsing: The first step is as follows:
    1. In contrast to class method parsing, if index C in Class_index is found to be a class rather than an interface in the interface method table, it is thrown directly java.lang.IncompatibleClassChangeError
    2. Find out if there is a match in C
    3. Find a match in the parent interface of C
    4. Fail, throw Java.lang.NoSuchMethodError
There is no access problem because the interface method is the default public InitializeIs the last step in the class loading process, the initialization phase is really starting to execute in the Java program Code preparation phase defined in the class, the variable has already been assigned the initial value of the system requirements, and during the initialization phase, the initialization phase is the execution class constructor < The process of the clinit> () method
    The
    • <clinit> () method is a combination of the copy action of all class variables in the compiler's auto-collection class and the statements in the static statement block . The compiler collects orders and statements in the same order as they appear in the source file , static statement blocks can only access variables defined before it, variables defined behind it, can only be assigned values, cannot be accessed
    • The <clinit> () method differs from the class's constructor <init> () and does not require an explicit call to the parent class constructor, and the virtual opportunity guarantees that <clinit> () of the parent class is completed before the child class . Therefore, the first <clinit> () method performed by a virtual machine is definitely java.lang.Object.
    • because the parent class <clinit> () method executes first, it means that the static statement defined in the parent class takes precedence over the child class's variable assignment operation . The
    • <clinit> () method is not required, and if a class does not have a static statement block and does not assign a value to a variable, it does not generate an operation that cannot use static statement blocks in the
    • interface, but still has variables that initialize the assignment, and therefore also generates < Clinit> () method, but unlike a class, the <clinit> () method of the interface does not need to perform the <clinit> () method of the parent interface . the Parent interface initializes only if the variables defined in the parent are used, and the interface's implementation class does not execute the <clinit> () method of the interface as it was initialized .
    • Virtual opportunity to ensure that the <clinit> () method of a class is properly locked in a multithreaded environment , if multiple threads initialize a class at the same time, then will only have one thread to execute the class's <clinit> () method, other threads block until the method finishes executing, and if there is a lengthy operation in the <clinit> () method of a class, can cause multiple processes to block , In practical applications, this blockage is often concealed.
class LoaderA binary stream describing this class is obtained through the fully qualified name of a class, and the code module that executes the action becomes the ClassLoader. Two classes are only interesting if the same ClassLoader is loaded, otherwise the two classes are not equal, even if the same class file is loaded with two classes of atoms, as long as the loader that loads them is different.。 Here the equality, including Equals,isassignablefrom (), isinstance () instanceof and so on. Parental delegation ModelThere are only two different class loaders: start the class loaderBootstrap ClassLoader), using C + +Implementation, is part of the virtual machine itself. The other is all other class loadersUse JAVAImplementation, independent of the JVM, and all inherits from the abstract class Java.lang.ClassLoader.Most Java programs use the following three types of system-provided ClassLoader:
    1. Start the class loader (Bootstrap ClassLoader), which will be stored in the <java+home>\lib directory, Or in the path developed by the-xbootclasspath parameter , and is recognized by the JVM ( only according to the name of the file, such as Rt.jar, if the name does not match, even if placed in the Lib directory will not be loaded ), Loaded into the virtual machine memory, the startup ClassLoader cannot be referenced directly by the Java program .
    2. The extension class loader , implemented by Sun.misc.launcher$extclassloader, is responsible for loading the <java_home>\lib\ext directory, or all class libraries in the path specified by the Java.ext.dirs system variable , developers can use the Extension class loader directly .
    3. The Application class loader (Application ClassLoader), implemented by Sun.misc.launcher$appclassloader. since this classloader is the return value of the Getsystemclassloader () method in ClassLoader , it is generally called the system ClassLoader . Responsible for loading the class library specified on the user classpath (ClassPath), developers can use the ClassLoader directly , if the application does not customize its own classloader, typically this is the default class loader in the program.
This graph represents the parent delegation model of the ClassLoader (parents delegation models). The parental delegation model requires that the rest of the ClassLoader should have their own parent classloader, in addition to the top-level startup load Class。 , where the parent-child relationship between class loaders is generally not implemented in an inherited relationshipBut working with composite relationshipsThe code that uses the parent ClassLoader. The parental delegation model is not a mandatory model, but the working process of recommending a parental delegation model is: when a class loader is subjected to a class load request, it does not first attempt to load the class, but instead delegates the request to its own parent ClassLoader to complete, and the child loader tries to load itself only when the parent loader indicates that it cannot complete the load request.。 The benefit of this model is to ensure that a range of classes must be loaded by a class loader, which guarantees that the same class in the program will not be loaded by different classloader. One of the main considerations in this way is to eliminate the security dimension by eliminating the use of classes with the same class name as the JRE to attempt to replace the existing JRE. destroying parental delegation modelsSo far, there have been three large-scale cases of destruction of the parental delegation model, since the parent delegation model was introduced after JDK1.2, and there were Java.lang.ClassLoader and ClassLoader at JDK1.0. In order to be forward compatible, JDK1.2 after Java.lang.ClassLoader added a new protected method Findclass (), again before the The only purpose for the user to inherit Java.lang.ClassLoader is to override the LoadClass () method, because the virtual machine calls the loader's private method loadclassinternal () when the class is loaded. The only logical way to do this is to call your own loadclass (). Instead of advocating rewriting the LoadClass () method after JDK1.2, you should write your class load logic into the Findclass () method, and in the logic of the LoadClass () method, if the parent class fails to load, it will call its own Findclass () method to complete the load. This ensures that the newly written class loads conform to the parental delegation rules. ( The default LoadClass () method calls the parent class to load first, and if it fails, the Findclass () method is called, and if the LoadClass () method is overridden in its own ClassLoader implementation, the parent delegate model is invalidated if it is not called .). The second destruction was caused by the flaw in the model itself, if the underlying class calls back the user's codeFor example, the Jndi service, which itself is loaded by the startup ClassLoader, but the purpose of Jndi is to centrally manage and locate resources, it needs to invoke the Jndi interface provider code implemented by the standalone vendor and deployed under the classpath of the application, but the startup ClassLoader does not recognize the code , in order to solve this problem, the threading Context class loader (thread ClassLoader) was introduced. This class can be set by the Setcontextclassloader () method of the Java.lang.Thread class, and if it is not set when the thread is created, it will inherit one from the parent thread if it is not set in the global scope of the application. The class loader defaults to the Application class loader. This setting, is actually to let the parent class can help load the class by specifying the sub-loader。 The third destruction is due to the user's pursuit of the dynamics of the program, such as HotSwap, hotdeployment in fact, the current industry de facto Java module Bad standard OSGi also destroys the parental delegation model, each program module (bundle) has its own class loader , when a bundle needs to be replaced, the bundle is replaced with the same loader to implement the hot substitution of the code. Original: http://raging-sweet.iteye.com/blog/1195036 Article recommendation: http://www.wfuyu.com/php/22254.html

Virtual machine class loading mechanism

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.