Good programmers share JVM Java class loading mechanism

JVM related concepts

  • the JDK <br> the JDK (Java Development Kit) Java Development Kit, is a collection of Java developers a set of procedures for compiling and debugging of programs.

  • jre <br> jre (Java Runtime Evironment) Java Runtime Environment, is a platform for running Java programs, all Java programs can only be executed in this platform.

  • the JVM <br> the JVM (the Java Virtual Machine) the Java Virtual Machine, is out of the virtual computer codes to simulate the functions performed by a computer, it has its own hardware architecture, such as: processor, stack, registers, etc., as well as their own a set of instruction on different operating systems can be installed JVM, enabling Java programs can execute on different operating systems, JVM is to achieve Java's cross-platform features.

JVM class loading process

We developed a Java program execution, you need to compile and then executed, JVM is responsible for the process of loading the class. <br> process class loader is divided into:

  1. load

  2. verification

  3. ready

  4. Resolve

  5. initialization

DETAILED class loading procedure

The following details under these procedures:

  1. Load <br> in the process of loading the class to complete:

    1. According to the full name of the qualifier class, obtain class binary stream, the stream can, jar files obtained from the class on the disk, can also be obtained from the network.

    2. Dynamic storage structure when the structure of the class of static storage operation into the method area

    3. In the heap memory to generate the corresponding java.lang.Class objects, a method as an inlet region

  2. Verify <br> After loading the class is completed, enter the verification process, which ensures that the information generated in the previous Class object will not endanger the safety of the JVM. <br> aspects need to be verified are:

    1. File format validation is to verify compliance with specifications Class byte stream file format, and can be the current version of virtual machine processing. The magic number, to verify whether 0xCAFEBABE; the major and minor version number is the current virtual machine processing range; if there is not constant type supported constant in the constant pool, etc., the main purpose is to ensure the verification phase input byte flow properly parse and stored in the process region, are authenticated at this stage, the byte stream will enter the storage method of the memory area, so that the latter three are based verification phase memory area configuration method performed.

    2. Metadata verification information is described bytecode semantic analysis, to ensure compliance with the information which describes the Java language specification. The validation may include: whether a parent class; whether the parent of this class inherit the inherited class allowed; if the class is not an abstract class, whether implements all the methods of its parent class or interface implementation requirements.

    3. Bytecode verification, the main task is the data flow and control flow analysis to ensure the method validation class at runtime will not make a virtual machine acts endangering safety. If the byte code method body does not pass a class bytecode verification, it is certainly a problem; but if a method body by the byte code verifier can not explain it necessarily safe.

    4. Verification of symbolic references, will take place in the virtual machine when symbolic references into direct reference, this conversion action will take place in the "analytical phase". Symbolic reference classes, fields and methods; validation symbol strings described by reference to the naming authority if a corresponding class can be found; whether there is a method in compliance with a method specified field descriptor class name and a simple description of the fields and accessibility (private, protected, public, default) whether the current class can be accessed.

  3. Preparation <br> preparation phase method allocates memory area for static class variables and assign default values.

    public static int count = 100;

    Such as: the above count variable in the preparation phase will be assigned to 0 and then assigned to 100 during initialization;

  4. Resolve <br> parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references.

    • Reference symbol (Symbolic Reference) <br> symbolic reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be positioned unambiguously as long as the goal to use. Symbol reference has nothing to do with the memory layout to achieve a virtual machine, the goal is not necessarily a reference has been loaded into memory.

    • A direct reference (Direct Reference) <br> direct reference may be a direct pointer to the object, a relative offset or indirectly targeted to handle targets. Direct reference to the memory layout is related to implementation of the virtual machine, if you have a direct reference, the target reference must already exist in memory.

  5. Initialization <br> class initialization is the final step in the process of loading the class, in front of the class loading process, in addition to other than by self-defined class loader to participate in the remaining action is completely dominated by the virtual machine control and user applications in the loading phase. To the initialization phase, really started classes defined in Java code. <br> initialization phase is performed class constructor <clinit> Process () method. <the clinit> () method is collected automatically by the compiler class assignment operation of all classes and static variables of the block of statements (static {} block) merge statements generated.

    So when to perform initialization it?

    1. Create an instance of the class

    2. Access class static variable (except the constant, final modified) reason: Constant a special variable, because the compiler treats them as property values rather than to treat.

    3. Access class static method

    4. The reflector (Class.forName ( "com.test.Person"))

    5. When initializing a class, the class has not yet found his father initialization, the first call to initialize the parent class

    6. When the virtual machine starts, the definition of the main () method to initialize the class

Code Cases

Understand the class loading mechanism, we look at a face questions:

public class MySingleton {

private static MySingleton singleton = new MySingleton();
public static int count1 = 0;
public static int count2;

private MySingleton(){
count1++;
count2++;
}

public static MySingleton getInstance(){
return singleton;
}

public static void main(String[] args) {
MySingleton singleton = MySingleton.getInstance();
System.out.println("count1-->"+MySingleton.count1);
System.out.println("count2-->"+MySingleton.count2);
}

}

The above results, the majority of students may think that two static variables are 1, the result of more accidents:


count1-->0
count2-->1

Why is this? Now we have to analyze:

  1. First we know will assign default values for static variables in the preparation phase class: <br> Singleton = null; count1 = 0; count2 = 0;

  2. When the getInstance static method call class, class initialization triggered, perform new MySingleton () constructor method call, then: <br> count1 is =. 1; count2. 1 =;

  3. Continue to initialize, assign values to variables, count1 assigned 0, count2 no assignment to retain the value 1, the result is: <br> count1 = 0; count2 = 1;

to sum up

JVM is the code of the computer simulation, with its own hardware and software, JVM enables Java classes to load and run specific loading process are: loading, validation, preparation, analytical, initializing five steps.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10974568.html