import java.net.URL; import java.io.File; import java.net.URLClassLoader; import sun.net.www.ParseUtil; /* * javac G1Checker2.java && mkdir -p foo && mv FooFoo.class foo/ * * java -cp . G1Checker2 * java -cp . G1Checker2 classloader * * java -cp . -XX:+PrintGCDetails -XX:+UseG1GC G1Checker2 * java -cp . -XX:+PrintGCDetails -XX:+UseG1GC G1Checker2 classloader */ public class G1Checker2 { static URLClassLoader u1, u2, u3; static Class c1, c2, c3; public static void main(String[] args) throws Exception { boolean classLoader = false; if (args.length > 0) { classLoader = "classloader".equals(args[0]); } String klassPath = "FooFoo"; ClassLoader loader = G1Checker.class.getClassLoader(); //loading a class referenced in the parent url loader Class theKlass; while (true) { File file = new File("foo"); file = file.getCanonicalFile(); URL urls[] = new URL[1]; urls[0] = ParseUtil.fileToEncodedURL(file); URLClassLoader urlClassLoader = new URLClassLoader(urls, loader); u3 = u2; u2 = u1; u1 = urlClassLoader; if (classLoader) { theKlass = urlClassLoader.loadClass(klassPath); } else { //loading it this way creates lots of references in the dictionary theKlass = Class.forName(klassPath, false, urlClassLoader); } theKlass.toString(); c3 = c2; c2 = c1; c1 = theKlass; //System.out.println("Loader = " + theKlass.getClassLoader()); } } } class FooFoo { }