In-depth understanding of JVM: class loader

A class loader

The class loading phase to obtain the description of such binary byte fully qualified class name by a class of this stream into jvm external action implemented to achieve this action code modules called "class loader."

For any class, you will need to establish the uniqueness of its jvm loaded by class loader and the class itself together. In other words, if you load a different type of class loader, then the two classes in the jvm is different.

Three types loader

JVM built three important ClassLoader, in addition to other BootstrapClassLoader by the Java class loader and implement all inherited from java.lang.ClassLoader:

  1. BootstrapClassLoader (boot class loader)  : top-level class loading, realized by C ++, is responsible for loading  %JAVA_HOME%/libjar package and class or directory, or  -Xbootclasspathall of the parameters specified in the class path.
  2. ExtensionClassLoader (extension class loader)  : primarily responsible for loading the directory  %JRE_HOME%/lib/ext jar package and class directory, or  java.ext.dirs jar package under system variables specified path.
  3. AppClassLoader (application class loader)  : for our users loader is responsible for loading all current jar packages and classes in the application classpath.

From the perspective of only two jvm class loader:

(1) boostrap ClassLoader: It's all right jvm part of c ++ implementation.

(2) the other is the other type of class loaders, implemented by the java, all integrated java.lang.classLoader abstract class.

Second, the parent delegation model

ClassLoader

Each class has a class loader that. System ClassLoder at work when using the default  parent delegation model  . That is when the class is loaded, the system will first determine whether the current class is loaded too. Has been loaded class will be returned directly, or will attempt to load. When loaded, the request will be delegated to the first parent class loader  loadClass() process, so ultimately all requests should be transferred to the top of the boot class loader  BootstrapClassLoader in. When the parent class loader can not handle, only to be handled by themselves. When the parent class loader is null, it will use a boot class loader  BootstrapClassLoader as parent class loader.

Note: The parent-child relationship between the class loader is not to inherit manner, but in combination (priority)

Source

private final ClassLoader parent; 
protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // 首先,检查请求的类是否已经被加载过
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {//父加载器不为空,调用父加载器loadClass()方法处理
                        c = parent.loadClass(name, false);
                    } else {//父加载器为空,使用启动类加载器 BootstrapClassLoader 加载
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                   //抛出异常说明父类加载器无法完成加载请求
                }

                if (c == null) {
                    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;
        }
    }

If you do not want to use the parent delegation model: the need for custom class loader, rewrite LoadClass () method, need to be integrated ClassLoader class.

Published 134 original articles · won praise 91 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_44588495/article/details/104130913