JVM knowledge - class loading mechanism associated test sites

Class loading mechanism associated test sites

1. Detailed class loading mechanism

Class loading refers to the class files compiled class byte code is read into memory, the method in which the area and create a corresponding Class object.

The class is loaded into loading link initialization , which also includes links verification, preparation, analytical three steps. We analyzed one by one:

  • Process file is loaded into memory. Finding such bytecode files by fully qualified name of the class, and create a Class object using the byte code file

  • Verification is to verify the contents of the file class. The purpose is to ensure that Class files that meet the current requirements of the virtual machine, the virtual machine will not jeopardize their own safety. It includes four types: a file format validation, metadata validation, bytecode verification, verification of symbolic references .

  • Preparation phase is memory allocation. Assign a class variable is the class of modified static variable memory, and set the initial value, it is noted here that the initial value is 0 or null, rather than the specific value set in code, the value of the code set is done in the initialization phase of. Also here it is not included with the final modification of static variables, because final at compile time will be allocated.

  • The main field is parsed parsing, interfaces, methods. The main pool is a symbolic constant references to the process of replacing direct references. Direct reference is the direct pointer to the object, relative offset and the like.

  • Finally Initialization: The main finishes executing the assignment of static block static variables. This is the final stage of the class loader, if they are loaded super class is not initialized, the first parent class is initialized.

When only the active use of the class, will be initialized, initialization trigger conditions include creating an instance of the class, when the class or static methods to access static variables, Class.forName () reflecting class, or a subclass is initialization time.

Life-cycle class, is loaded from class to class instance creation and use, GC can be uninstalled and then recovered when the class object is no longer used. Here we must note that, by the java virtual machine comes with three types of class loader loads the entire life cycle of the virtual machine will not be unloaded, only user-defined class loader loaded class can be unloaded .

2. Detailed class loader

java loader comes with three types are: bootstrap class loader to start, expand class loader and loader applications, also known as system loader. Orange text on the right of the figure shows various types of loader loads the corresponding directory. Start class loader loads the class java home in the lib directory under the extended class loader is responsible for loading ext directory of the application class loader loads the classpath specified directory.

In addition, the class loader can customize.

Java class loader delegate parents use patterns, namely a class loader when loading the class, put the request entrusted to their parent class loader to perform, if the parent class loader there parent class loader, we continue to commission up until the top of the boot class loader, as shown by the arrows in the blue direction. If the parent class loader class loader to complete, it returns successfully, if the parent class loader can not finish loading, then the child loader will try to load your own.

The benefits of this model of delegated parents, to avoid a repeat of the class load, while also avoiding the core java API has been tampered with.

Reproduced in: https: //juejin.im/post/5cefdcf86fb9a07eeb138d86

Guess you like

Origin blog.csdn.net/weixin_33721427/article/details/91465635