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();
}
c = null;
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?