Class loading
Class loading refers to reading binary data from the class .class file into memory, putting it in the method area of the runtime data area, and then creating a java.lang.Class object in the heap area to use Encapsulate the data structure of the class in the method area.
How to load .class files:
1. Load directly from the local system
2. Download the .class file through the Internet
3. Load .class files from zip, jar, etc.
4. Extract .class files from proprietary database
5. Dynamically compile Java source files into .class files
The final product of the class loading is a Class object located in the heap area.
The Class object encapsulates the data structure of the class in the method area and provides Java programmers with an interface to access the data structure in the method area.
There are two types of class loader loaders:
1. The loader that comes with Java virtualizer
Root class loader (Bootstrap)
Extension class loader (Extension)
System class loader or application loader (System)
The latter two loaders are implemented in Java, and the root class loader is written in C++, and programmers cannot obtain this class in Java code.
2. User-defined class loader
Subclass of java.lang.ClassLoader
Users can customize the loading method of the class
The class loader does not need to wait until a class is first actively used before loading it.
The JVM specification allows class loaders to preload a class when they expect it to be used. If a .class file is missing or there is an error during the preloading process, the class loader must not be used by the program for the first time. Report an error (LinkageError). If this class has not been actively used by the program, the class loader will not report an error.
Class uninstallation mechanism
The life cycle of a class. When the Sample class is loaded, connected, and initialized, its life cycle begins.
When the Class object representing the Sample class is no longer referenced, that is, it cannot be touched, the Class object will end the life cycle, and the data of the Sample class in the method area will also be unloaded, thus ending the life cycle of the Sample class.
It can be seen that when a class ends its life cycle depends on when the Class object representing it ends its life cycle.
Reference relationship loader and Class objects:
In the internal implementation of the class loader, a Java collection is used to store references to the loaded class.
On the other hand, a Class object always references its class loader. Call the getClassLoader() method of the Class object to get its class loader.
It can be seen that there is a bidirectional relationship between the Class instance and the loader that loads it.
Class object, class object, class instance object:
An instance of a class always refers to the Class object representing the class.
The getClass() method is defined in the Object class, which returns a reference to the Class object representing the class to which the object belongs.
In addition, all Java classes have a static property class that references the Class object representing this class.
Uninstalling the class
Classes loaded by the class loader that comes with the Java virtual machine will never be uninstalled during the life cycle of the virtual machine.
As mentioned earlier, the class loaders that come with Java virtual machines include root class loaders, extension class loaders and system class loaders.
The Java virtual machine itself always references these class loaders, and these class loaders always refer to the Class objects of the class they load, so these Class objects are always accessible.
Classes loaded by user-defined class loaders can be unloaded.
Specific examples
The loader1 variable and the obj variable indirectly apply the Class object representing the Sample class, while the objClass variable directly references it.
If the three reference variables on the left side of the above picture are set to null during the program operation, the Sample object ends its life cycle, the MyClassLoader object ends its life cycle, and the Class object representing the Sample class also ends its life cycle. The Sample class is in the method area The binary data inside is unloaded.
When it is necessary again, it will check whether the Class object of Sample class exists. If it exists, it will be used directly and will not be reloaded; if the Sample class does not exist, it will be reloaded, and a new representative will be generated in the heap area of the Java virtual machine. Class instance of Sample class (you can check whether it is the same instance through the hash code).