18 class namespace and unloading Detailed and use jvisualvm

Namespace class and unloading Detailed and use jvisualvm

Namespaces:

  • Each class loader has its own namespace, the namespace by the class loader and all the parent class loader loaded classes .
  • In the same namespace, the two classes [full name of the class (including package of) the same does not occur].
  • In a different namespace, [there may be a full class name (including package of) the same] two classes.

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(loader1,"loader2"); 使loader1作为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);
}
运行结果:
   findClass invoked:Jvm.MyTest10
   class loader name:loader1
   class:1173230247
   MyTest10 static block
   Jvm.MyTest10@330bedb4
----------------------------------
   class:1173230247
   Jvm.MyTest10@2503dbd3
  • Result analysis:
    • (Delete the class file, a copy of the desktop Jvm corresponding document) operating can be seen from the results, in the horizontal part of the upper part is loaded MyTest16 the process loader1, loader1 find the parent class loader (the system class loader) loads , not loaded, then loader1 own load.
    • The following part is the horizontal line, loader2 will go first to their parent class loader (loader1) load, due loader1 has been loaded, so the direct method returns an object [because of loadClass source, when calling the parent class, parent class loader will first call findLoadedClass (String) to determine if they will be loaded class has a loader]

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);
}
运行结果:
   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

Result analysis:

  • (Class files in the deleted file out) are loader1 their feelings!

Class unloading

  • When MySample class is loaded, the connection and initialization, its life cycle begins. When the Class object represents MySample class are no longer referenced, that is, untouchable, Class object will end the life cycle, MySample class data in the method area will be unloaded, thus ending the life cycle MySample class.
  • When the end of the life cycle of a class, depending on behalf of its Class object when the end of the life cycle.
  • It comes from the Java virtual machine class loader to load classes in virtual machine lifecycle, and always will not be uninstalled.
    • It has been introduced, Java virtual machine includes a class loader comes root class loader extension class loader, the system class loader. Java virtual machine itself will always reference the class loader, which always refers to the class loader will load the Class object of their class, so these Class object is always accessible.
  • By a user-defined class loader to load a class can be unloaded.
    • Such as: Sample class loaded by loader1, the internal implementation of the class loader, with reference to the presence of a set of Java classes are loaded. On the other hand, a Class object will always refer to it in class loader, call the Class object getClassLoader () method, you can get it in the class loader. Thus a two-way relationship between the association, Class instance loader1 representatives Sample class.
    • ** instance of a class is always referred to on behalf of Class objects of this class. ** defined getClass () method in the Object class, representative of this method returns a reference to the object's class Class. In addition, Java classes used has a static property class, it refers to representatives of Class objects of this class.

Example: [class unloading -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();   //回收

   Thread.sleep(10000);    //<-----为了方便使用jvisualvm查看加载类和卸载类的过程
   
   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();
}
运行结果:
      findClass invoked:Jvm.MyTest10
      class loader name:loader1
      class:1173230247
      MyTest10 static block
      Jvm.MyTest10@330bedb4

      [Unloading class Jvm.MyTest10 0x00000007c0061828]    <----打印类的卸载
      findClass invoked:Jvm.MyTest10
      class loader name:loader1
      class:2125039532
      MyTest10 static block
      Jvm.MyTest10@12a3a380

Here Insert Picture Description

Published 25 original articles · won praise 0 · Views 1455

Guess you like

Origin blog.csdn.net/qq_40574305/article/details/104793328