The details of class namespace and unload and the use of JVM
Namespace:
- Each class loader has its own namespace, which consists of the class loader and the classes loaded by all the parent class loaders.
- In the same namespace, [two classes with the same full name (including the package name of the class) will not appear].
- In different namespaces, [two classes with the same full name (including the package name of the class) may appear].
Or MyTest16:
public static void main(String[] args) throws Exception { MyTest16 loader1 = new MyTest16("loader1"); loader1.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz = loader1.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz.hashCode()); Object object = clazz.newInstance(); System.out.println(object); System.out.println(); //The construction method is changed to MyTest16(loader1,"loader2"); make loader1 the parent loader of loader2 MyTest16 loader2 = new MyTest16(loader1,"loader2"); loader2.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz1 = loader2.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz1.hashCode()); Object object1 = clazz.newInstance(); System.out.println(object1); } //Operation result: findClass invoked:Jvm.MyTest10 class loader name:loader1 class:1173230247 MyTest10 static block Jvm.MyTest10@330bedb4 ---------------------------------- class:1173230247 Jvm.MyTest10@2503dbd3
- Results analysis:
- (delete the class file and copy a corresponding file in the desktop Jvm) from the running result, it can be seen that the part above the horizontal line belongs to the process of loader1 loading MyTest16. Loader1 finds the parent class loader (system class loader) to load, but it can't, and then loader1 loads by itself.
- The lower part of the horizontal line is that loader2 will first find its own parent class loader (loader1) to load, because loader1 has been loaded, so it will directly return the object (because in the loadClass source code method, when calling the parent class, the parent class loader will first call findLoadedClass(String) to determine whether the class to be loaded has been loaded)
Or MyTest16:
public static void main(String[] args) throws Exception { MyTest16 loader1 = new MyTest16("loader1"); loader1.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz = loader1.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz.hashCode()); Object object = clazz.newInstance(); System.out.println(object); System.out.println(); MyTest16 loader2 = new MyTest16(loader1,"loader2"); loader2.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz1 = loader2.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz1.hashCode()); Object object1 = clazz.newInstance(); System.out.println(object1); System.out.println(); MyTest16 loader3 = new MyTest16(loader2,"loader3"); loader2.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz2 = loader3.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz2.hashCode()); Object object2 = clazz.newInstance(); System.out.println(object2); } //Operation result: findClass invoked:Jvm.MyTest10 class loader name:loader1 class:1173230247 MyTest10 static block Jvm.MyTest10@330bedb4 class:1173230247 Jvm.MyTest10@2503dbd3 class:1173230247 Jvm.MyTest10@4b67cf4d
Results analysis:
- (delete the class file under the out file) it's how loader1 feels!
Class uninstall
- When the MySample Class is loaded, connected, and initialized, its life cycle begins. When the Class object representing MySample Class is no longer referenced, that is, untouchable in time, the Class object will end its life cycle, and the data of MySample Class in the method area will also be unloaded, thus ending the life cycle of MySample Class.
- When a Class ends its life cycle depends on when the Class object representing it ends its life cycle.
- The classes loaded by the class loader of the Java virtual machine will never be unloaded in the life cycle of the virtual machine.
- As mentioned earlier, the classloader of Java virtual machine includes root classloader, extension classloader and system classloader. The Java virtual machine itself will always reference these Class loaders, and these Class loaders will always reference the Class objects of the classes they load, so these Class objects are always accessible.
- Classes loaded by user-defined class loaders can be unloaded.
- For example, the Sample Class is loaded by loader1. In the internal implementation of the Class loader, a Java collection is used to exist the reference of the loaded Class. On the other hand, a Class object always references its classloader. If you call the getClassLoader() method of the Class object, you can get its classloader. It can be seen that the Class instance representing the Sample Class has a bidirectional association with loader1.
- **An instance of a class always references a class Object that represents that class. **The getClass() method is defined in the Object class, which returns a reference to the class Object representing the class to which it belongs. In addition, all Java classes used have a static attribute class, which refers to the class Object representing the class.
Instance: [unloading of class - XX:+TraceClassUnloading]
public static void main(String[] args) throws Exception { MyTest16 loader1 = new MyTest16("loader1"); loader1.setPath("C:\\Users\\admin\\Desktop\\"); Class<?> clazz = loader1.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz.hashCode()); Object object = clazz.newInstance(); System.out.println(object); System.out.println(); loader1 = null; clazz = null; object = null; System.gc(); //recovery Thread.sleep(10000); //< for the convenience of using the JVM to view the process of loading and unloading classes loader1 = new MyTest16("loader1"); loader1.setPath("C:\\Users\\admin\\Desktop\\"); clazz = loader1.loadClass("Jvm.MyTest10"); System.out.println("class:"+clazz.hashCode()); object = clazz.newInstance(); System.out.println(object); System.out.println(); } //Operation result: findClass invoked:Jvm.MyTest10 class loader name:loader1 class:1173230247 MyTest10 static block Jvm.MyTest10@330bedb4 [Unloading class Jvm.MyTest10 0x00000007c0061828] <----Print class unload findClass invoked:Jvm.MyTest10 class loader name:loader1 class:2125039532 MyTest10 static block Jvm.MyTest10@12a3a380