Class unloading in java

Posted by java_geek on Stack Overflow See other posts from Stack Overflow or by java_geek
Published on 2010-04-01T09:22:05Z Indexed on 2010/04/01 9:33 UTC
Read the original article Hit count: 285

When a classloader is garbage collected, are the classes loaded by it unloaded? When the JVM is running is verbose mode, all the loaded classes are o/p. Similarly will the JVM log when it unloads a class?

I wrote a custom class loader to test this, but could not see any verbose log for unloading of the classes.

CustomClassLoader loader = new CustomClassLoader(new URL[]{}, CustomClassLoader.class.getClassLoader()); loader.addURL("D:\workspace\ClassLoaderTest\implementation.jar");

    Class c = null;
    try {
        c = Class.forName("Horse",false,loader);
        if (c != null) {
            try {
                Animal animal = (Animal)c.newInstance();
                animal.eat();
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    loader = null;
    byte[] b = new byte[58*1024*1024];
    System.gc();
    ClassLoadingMXBean clBean = ManagementFactory.getClassLoadingMXBean();
    System.out.println("Number of classes currently loaded " + clBean.getLoadedClassCount());
    System.out.println("Number of classes loaded totally " + clBean.getTotalLoadedClassCount());
    System.out.println("Number of classes unloaded " + clBean.getUnloadedClassCount());

Even the ClassLoadingMXBean gives number of unloaded classes as 0.

How can i know that a class is unloaded when the class loader is GCed?

© Stack Overflow or respective owner

Related posts about java

Related posts about garbage-collection