class loading

 

The entire life cycle of a class starts from being loaded into the virtual machine memory and unloads the memory:

 




 

First, the timing of class loading

 

1. When instantiating an object, reading or setting a static field of a class, or calling a static method of a class;

 

2. When using the method of the java.lang.reflect package to make a reflection call to the class;

 

3. When a class is initialized and it is found that its parent class has not been initialized, it is necessary to trigger the initialization of its parent class first;

 

4. When the virtual machine starts, the user needs to specify a main class to be executed, and the virtual machine initializes this main class first;

 

5. When using the dynamic language of jdk1.7,

 

The difference between the initialization of an interface and the initialization of a class: when a class is initialized, all its parent classes must have been initialized, but the interface is different, it does not necessarily require all its parent interfaces to be initialized, but only when it is actually used. It is only initialized when the parent interface is used.

 

The process of class loading

 

1. Loading: Convert the static storage structure represented by the binary byte stream of a class into the runtime data structure of the method area, and correspondingly generate the class object in the memory as the access entry for the respective data of the class in the method area. Specific possible load files: (1) read from zip, such as jar\ear\war (2) read from network, such as applet; (3) generated by runtime calculation, such as reflect; (4) from other files read, like jsp

 

2. Verification: Verify that the information contained in the byte stream of the class file meets the requirements of the current virtual machine

 

3. Preparation: mainly allocate memory for class variables and set their initial values ​​(note: the memory is allocated in the method area, and the actual value of the variable can only be mapped to the variable after compilation)

 

4. Analysis: mainly the virtual machine replaces the symbolic reference of the constant pool with a direct reference

 

5. Initialization: The actual start of execution of the Java program code defined in the class

 

Third, the class loader

 

Comparing whether two classes are "equal" only makes sense if the two classes are loaded by the same class loader, otherwise, even if the two classes come from the same class file, they are loaded by different class loaders of the same virtual machine. Loading, the two classes are also not equal.

 

Parent delegation model : When a class loader receives a class loading request, it first delegates the request to the parent class loader to complete, ensuring that all loading requests are ultimately loaded by the startup class loader. It avoids the user's own definition of the loaded object, which is loaded by other loaders, which makes the java program become a mess.

 



 

 

View the loadClass() method of java.lang.ClassLoader, logic:

 

1. First check if it has been loaded

 

2. If not loaded, call the loadClass method of the parent loader

 

3. If the parent loader is empty, the startup class loader is used by default to load

 

4. If the loading fails, throw a ClassNotFoundException, and then call your own findClass method to load

  protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

 
Breaking the parental delegation model

 

The first time: the introduction of the parent delegation model, when jdk1.2

 

The second time: solve some basic classes to call back to the user's code, such as JNDI services

 

The third time: solve the pursuit of program dynamics, such as code hot swap (HotSwap), module hot deployment (Hot Deploymnet), understand the class loader of osgj to grasp the essence of class loading

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326300766&siteId=291194637