Java class loading process

Source: Internet
Author: User

Classes start from being loaded into the JVM, and until unloaded, the entire lifecycle consists of seven phases: load, validate, prepare, parse, initialize, use, and unload. The class loading process includes five stages of loading, validating, preparing, parsing, and initializing.

The task of the ClassLoader is to read the binary bytes of this class into the JVM based on the fully qualified name of a class, and then convert to a Java.lang.Class object instance corresponding to the target class. The Bootstrapclassloader, Extclassloader, and Appclassloaderdefineclass methods convert byte arrays of bytecode into classes of class object instances, if they are to be linked when the class is documented in the JVM. Then you can call the Resolveclass method.

The parent-delegate model, the loading mechanism of the Contract class loader

 

1, loading simply said, the class loading stage is the class loader is responsible for the full qualified name of a class to read this class of binary bytes to the inside of the JVM, and stored in the runtime memory area of the method area, It is then converted to a Java.lang.Class object instance corresponding to the target type (the Java Virtual Machine specification is not explicitly required to be stored in the heap, but the hotspot chooses to store the class-to-play that is stored in the method area), and the class object will later be used as the data of the class in the method area. Access the portal. 2, link link phase to do is to load into the JVM's binary byte stream data information into the runtime state of the JVM, through the validation, preparation and resolution of three stages. 1), verify that the authentication class data information conforms to the JVM specification, is a valid bytecode file, the verification content covers the format validation, semantic analysis, operation verification, etc. of the class data information. Format validation: Verify that the class file specification semantic validation is checked: Checks whether a type that is marked as final contains subclasses, checks the final method in a class to overwrite the quilt class, and ensures that there are no incompatible method declarations between the parent and child classes, such as the same method signature, However, the return value of the method is different) operation validation: The data in the operand stack must be properly manipulated, perform validation on various symbol references in the constant pool (usually performed in the parsing phase, check whether the fully qualified name described in the regal reference is anchored to the specified type, and whether access modifiers for class member information allow access, etc.) 

2), prepare to allocate memory space for all static variables in the class, set an initial value for it (because the object has not been produced, the instance variable is not in this operation)3), resolve to convert all the symbol references in the constant pool to direct references (Get class or field, A pointer or offset in memory to invoke the method directly), which can be executed after initialization. 3.Initialize the code that identifies all the static keywords in a class uniformly, and if a static variable is executed, the initial value set before the preparation phase is overwritten with the user-specified value, and if the static code block is executed, in the initialization phase, The JVM executes all the operations defined in the static code block. All class-Variable initialization statements and static code blocks are placed in the collector by the front-end compiler at compile time and stored in a special method , which is the <clinit> method, the class/interface initialization method. The function of this method is to initialize the variable in one, overwriting the initial value set in the preparation phase with the user-specified value. The <clinit> method cannot be called by any byte code such as invoke, because the method can only be called by the JVM during class loading. If the parent class has not been initialized, the parent class is first initialized, but the <clinit> method of calling the parent class is not displayed inside the <clinit> method, and the JVM is responsible for guaranteeing that the <clinit> method of a class is executed before the Its parent class <clinit> method has been executed. The JVM must ensure that a class is initialized and, if it is multithreaded, it needs to be initialized at the same time, only one of the threads can be allowed to initialize it, and the remaining threads must wait until the active thread finishes initializing the class to notify other threads that are waiting. 

Resources:

1190000004597758

http://blog.csdn.net/ns_code/article/details/17881581

http://zyjustin9.iteye.com/blog/2092131

http://blog.csdn.net/u013256816/article/details/50829596

Java class loading process

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.