jvm (5): class loading mechanism

Class loading timing

Java Virtual Machine specification does not enforce constraints like the first phase of the timing of the loading process, but for the initialization phase 5 in the class must be the case initiated by the strict rules now:

  1. Encounter new, getstatic, putstatic invokestatic or 4 when this bytecode instruction, if the class is not been initialized, initialization is required to trigger it.
    • Use the new keyword to instantiate an object
    • Read or set a static field of a class (the final modification, the results have been compiled into a static field of the constant pool except)
    • Call the static method of a class
  2. Use java.lang.reflectpacket classes when the reflection method is called.
  3. When initializing a class, if you find the parent class has not been initialized, you need to trigger initialization of the parent class.
  4. When the virtual machine starts, the user needs to specify a main class to be executed, the virtual machine to initialize this master class.
  5. If a java.lang.invoke.MethodHandlefinal example of the analysis result REF_getStatic, REF_putStatic, REF_invokeStaticthe method handle, and this method handle class corresponding not been initialized, initialization is required to trigger it.

Class load

load

  1. Such acquired binary byte stream through a fully qualified name of the class.
  2. This represents the byte stream of static storage structure into a run-time data structure area method.
  3. Generating in memory a representative of the class of java.lang.Classobjects, a method for accessing the various data entry areas of this class.

verification

Objective: To ensure information Class file byte stream meets the requirements contained in the current virtual machine and the virtual machine will not jeopardize their own safety.

If the validation input byte stream does not conform to the constraint Class file format, the virtual machine should throw an java.lang.VerifyErrorexception or exception child.

  1. File format validation
  2. Metadata validation
    • Metadata information such semantic check.
  3. Bytecode verification
    • The data flow and control flow analysis to determine the semantics of the program is legitimate, logical.
  4. Verification of symbolic references
    • It will take place in the virtual machine into a symbolic reference when directly referenced.

ready

Formally allocate memory for class variables and set the initial value of the class variable order. Class variables are used by the memory area allocated in the process.

Is a class variable (static variables modified), do not include the instance variables! ! !

The initial value is typically zero. ConstantValueProperty is an exception.

Resolve

The constant pool of virtual machine symbols replace references to a direct reference.

The main analysis operation for the class or interface, fields, methods class, interface method, type method, and calls the method handle 7 points class qualifiers for symbolic references.

initialization

Actually starts executing Java code (bytecode) defined in the class.

To initialize class variables and other resources in accordance with procedures specified by the programmer subjective plan.

Initialization phase is performed class constructor <clienit>()method process.

<clienit>()Is collected automatically by the compiler class for all class variables assignment operation and the static block of statements statements merger, the collection order is order of appearance in the source file statement. Therefore, the definition of static variable after block of statements, statements in the block can be assigned but can not access. It need not be displayed to call the parent class constructor, therefore, block static statements defined in the parent class assignment superior subclass.

Class loader

For any class, we need to establish its uniqueness in the Java virtual machine by loading its class loader and the class itself together.

Parents committee model

Two types of loader

  • Start class loader is part of its own virtual machine. The virtual machine recognition library loaded into the virtual machine's memory.
  • Other class loader, implemented in Java, independent of external virtual machine

In addition to the parent delegation model requires top-level boot loader class, the rest of the class loader should have its own parent class loader. Parent-child relationship is achieved through a combination of.

If a class is loaded received a request to load a class, it first does not own to try to load this class, but to delegate this request to the parent class loader to complete each level of the class loader is true, so all the final loading request should be sent to the top of the boot class loader, only when the parent loader to load the feedback they can not complete the request, the child loader will try to load your own.

Objective: guaranteed the same class

Guess you like

Origin www.cnblogs.com/angelica-duhurica/p/11363189.html