JVM class unloading

1, when a class is loaded, the connection and initialization, its life cycle begins. When the Class object that represents the class are no longer referenced, that is inaccessible when, Class object will end the life cycle, this type of data in the method area will be unloaded, thus ending the life cycle of this class.

2, when the end of the life cycle of a class, depending on behalf of its Class object when the end of the life cycle.

3, by the Java virtual machine class loader comes loaded classes in virtual machine lifecycle, and always will not be uninstalled. It has been introduced, Java Virtual Machine classloader carrying loader includes a root, and the extended class loader system loader. Java virtual machine always refers to the class loader, which class loader will always refer to the Class object to which they are loaded, so these Class object is always accessible.

4, a user-defined class loader to load a class can be unloaded.

5, when running the above procedure, the Sample class loaded by loader1. Internally the class loader, with reference to store a set of Java classes are loaded. On the other hand, a Class object will always refer to it class loader, call the Class object getClassLoader () method, you will get its 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, this method returns the Class object representing a reference object belongs. In addition, all of the Java class has a static property class, it refers to representatives of Class objects of this class.

6, example unloading class

 

public class MyTest161 extends  ClassLoader{

    private String className;

    //table of Contents
     private String path;

    private final String fileExtension = ".class";

    public MyTest161(String classLoadName){
        super (); // the system class loader as parent class loader loader
        this.className = classLoadName;
    }

    public MyTest161(ClassLoader parent, String classLoadName){
        super (parent); // Displays the parent class loader loader device
        this.className = classLoadName;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public String toString() {
        return "[" + this.className + "]";
    }

    @Override
    protected Class<?> findClass(String clasName) throws ClassNotFoundException {
        System.out.println("findClass invoked:" + clasName);
        System.out.println("class loader name: " + this.className);
        byte[] data = this.loadClassData(clasName);
        return  this.defineClass(clasName,data, 0, data.length);
    }

    private byte[] loadClassData(String className){
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        try{
            className = className.replace(".","//");
            //System.out.println("className:" +this.className);
            is = new FileInputStream(new File(this.path + className + this.fileExtension));
            baos = new ByteArrayOutputStream();
            int ch = 0;
            while ( -1 != (ch = is.read())){
                baos.write(ch);
            }
            data = baos.toByteArray();

        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            try {
                is.close();
                baos.close();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }

        return  data;

    }

    public static void main(String[] args)  throws Exception{
        MyTest161 loader1 = new MyTest161("loader1");
        loader1.setPath("D:/temp/");
        Class<?> clazz = loader1.loadClass("com.example.jvm.classloader.MyTest1"); //
        System.out.println("class:" + clazz.hashCode());
        Object object = clazz.newInstance();
        System.out.println(object);
        loader1 = null;
        Clazz = null;
        object = null;
        System.gc (); // garbage collection. The actual development will not use
        System.out.println();

        loader1 = new MyTest161("loader1");
        loader1.setPath("D:/temp/");
        clazz = loader1.loadClass("com.example.jvm.classloader.MyTest1"); //
        System.out.println("class:" + clazz.hashCode());
        object = clazz.newInstance();
        System.out.println(object);
        System.out.println();



    }



}

 

  Set JVM parameters -XX: + TraceClassUnloading

operation result:

findClass invoked:com.example.jvm.classloader.MyTest1
class loader name: loader1
class:21685669
com.example.jvm.classloader.MyTest1@7f31245a

findClass invoked:com.example.jvm.classloader.MyTest1
class loader name: loader1
class:1173230247
[Unloading class com.example.jvm.classloader.MyTest1 0x0000000100061028]
com.example.jvm.classloader.MyTest1@330bedb4

  

 

Guess you like

Origin www.cnblogs.com/linlf03/p/11020251.html