Read one article class loading mechanism

Process according to class

A plurality of files compiled java package jar package that can be run, running the final main program function initiates a java command from the main class, the main class where the first need to be loaded by the class loader jvm.
Master class during operation if you use the other classes, these classes will gradually be loaded.
Note, jar bag class is not loaded all at once, it is used only when loaded.

Loaded from the class to the process used by the following steps:
loading, verification, preparation, resolution, initialization, use, uninstall

  • Load: Find and reads the IO byte code files, only using the loaded class, for example, on the hard disk: main class method calls, and so new new objects;
  • Validation: the correctness of the checksum byte code file;
  • Preparation: static variables to allocate memory class, and given a default value;
  • Analysis: The symbol references replaced direct reference, which will stage a number of static methods (symbolic references, such as the main method) replacement, etc. (direct quote) to point to where the data memory pointer or handle, which is static linking process that is during the class described completed. Dynamic linking is completed during the run will be replaced by a direct reference symbol references.
  • Initialization: initialize static class variables specified value, perform a static block.

    Class loader

    The above class loading process is mainly achieved by the class loader, java, there are several class loader.
  • Start class loader: responsible for loading the core libraries support at the lower amount of JREd lib directory JVM run;
  • Extension class loader: the support is responsible for loading the JVM execution located ext JRE lib directory JAR extended class package directory;
  • Application loader: responsible for loading class package under ClassPath path, the main application class is loaded;
  • Custom loader: responsible for loading the user-defined class package path;

Describes a class inherits java.lang.ClassLoader class, there are two methods core, and the loadClass findClass.

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;
    }
}

findClass default implementation method is thrown, so we custom class loader is mainly rewrite findClass method.

protected Class<?> findClass(String name) throws ClassNotFoundException {
    throw new ClassNotFoundException(name);
}

Parents delegate mechanism

jvm class loader is paternity layer structure.

When this class is loaded parents delegate mechanism, a class record, will first delegation parent class loader to find the target, you can not find entrust the upper parent loader to load, if at all parent loader to load classes and then their paths are not looking to target class, then find and load the target class of its own class loader path.

For example: Math class, will find the first application class loader loads the application loader will first commissioned the extension class loader loads, the extension class loader entrust the boot class loader, top boot loader loads the class in its own class path where to find for a long time could not find the Math class, then return down load request Math class, extended class loader to load their own receive a reply, looking for a long time in their own class loaders could not find the path in the Math class, the ED Math class to return the next load request to the application class loader, the application class loader then found the Math class in its own class load path, so he loaded himself.

Parents delegate said mechanism is simple: find someone to father loaded, not loaded by the father and then himself.

I'm not concerned about the number of the public?

  • Sweep end the two-dimensional code number [of public concern Xiaoqiang advanced road] can receive as follows:
  • Learning materials: 1T Video Tutorial: front and rear end covers Javaweb instructional videos, machine learning / AI instructional videos, Linux system tutorial videos, IELTS video tutorials;
  • More than 100 books: containing C / C ++, Java, Python programming language must see three books, LeetCode explanations Daquan;
  • Software tools: Most software includes almost you might use in programming the road;
  • Project Source: 20 JavaWeb source projects.
    Advanced small strong way two-dimensional code

Guess you like

Origin www.cnblogs.com/xiaoqiang-code/p/11526107.html