jvm (2) --- class loading mechanism

1. Load Class : class loader to load the class files into the virtual machine's memory
load: Find the read byte code file on the hard disk through the IO
connections: a check is performed, preparation, analysis step
calibration: calibration bytecode the correctness of files
preparation: static class variables to allocate memory, and given a default value
analysis: all classes other class loader loaded referenced class
initialization: initializing static variable of the class specified value, perform a static code block

 

2. The class loader :

Start class loader (BootstrapClassLoader): responsible for loading the JRE core class libraries, such as rt.jar under jre goal, charsets.jar and other
extension class loader (ExtensionClassLoader): responsible for loading the JRE extension directory ext classes in JAR package
system class loader device (ApplicationClassLoader): responsible for loading classes in the package ClassPath path
custom loader (CustomClassLoader): responsible for loading the user-defined class package path

Performing the following code, see corresponding class loader:

package com.nijunyang.spring;


import com.sun.crypto.provider.BlowfishCipher;

/**
 * @author: create by nijunyang
 * @date:2019/6/16
 */
public class ClassLoaderTest {
    public static void main(String[] args)
    {
        System.out.println(String.class.getClassLoader());
        System.out.println(BlowfishCipher.class.getClassLoader().getClass().getName());
        System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
        System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());
    }
}

 

   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can see the String class loader is null, because String in java core package rt.jar inside there is something that is loaded by the boot class loader, and start class loader is implemented by the C / C ++, and there is simply not the JDK . 
BlowfishCipher class loader is ExtClassLoader, and this class is the Jre extension classes in the directory ext jar package is located
--------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------

 

3. The class loading mechanism - the overall commission appointed mechanisms and parents

Delegate overall : When a ClassLoader loading a class, unless another display ClassLoader, and such depends also referenced class loaded by the ClassLoader, for example, rely A B, then the loading of the loader B and A same.

Parents delegate : Refers to delegate the parent class loader to find the target class, find the next occurrence will not find in their path and load the target class, said the feeling of the parent class loader is not very appropriate, because there is no relationship between this layer, feel more like a superior-subordinate relationship. Commissioned by the higher class loader to find the target.

Parents delegated benefits:

1.避免类重复加载,上级加载了,下级就不需要再加载

2.因为核心API都有固定的加载器,可以防止核心库被篡改: 自定义一个String类,同样在java.lang包下,运行代码会发现启动报错,实际加载的java.lang.String类并没有main方法,说明实际加载的String类并不是我们自定义的String类,而是因为双亲委派机制,往上委托到启动类加载器,去加载rt.jar里面的String类

 

4.类加载过程

实际上jvm对class文件是按需加载的,需要的时候才加载(运行时动态加载),并非一次全部加载进去,jvm启动参数加上:-verbose:class 查看类加载

package com.nijunyang.spring;


/**
 * @author: create by nijunyang
 * @date:2019/6/16
 */
public class ClassLoaderTest {
    static {
        System.out.println("静态块执行");
    }
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        System.out.println(System.currentTimeMillis());
        new A();
        System.out.println("A对象创建完毕");
        new B();
    }
}

 

从结果中我们可以看到

1.从本地/E:/IdeaProject/tuling/spring/target/classes/下面去加载当前运行类ClassLoaderTest.class文件

2.初始化类,执行静态代码块

3.main方法运行,睡了5s之后打印当前时间,当我们new A的时候才去本地加载A.class文件到内存,再实例化A

4.A创建完毕之后,再去加载B.class,完成B的初始化

 

Guess you like

Origin www.cnblogs.com/nijunyang/p/11037605.html