Skip to content

Instantly share code, notes, and snippets.

@leonardocordeiro
Last active January 27, 2017 02:23
Show Gist options
  • Save leonardocordeiro/8a200b8e357faba07cb3af59e71b6b2c to your computer and use it in GitHub Desktop.
Save leonardocordeiro/8a200b8e357faba07cb3af59e71b6b2c to your computer and use it in GitHub Desktop.
Class LeakSimulator
public class ClassMetadataLeakSimulator {
private static Map<String, ClassA> classLeakingMap = new HashMap<String, ClassA>();
private final static int NB_ITERATIONS_DEFAULT = 50000;
/**
* @param args
*/
public static void main(String[] args) {
int nbIterations = (args != null && args.length == 1) ? Integer.parseInt(args[0]) : NB_ITERATIONS_DEFAULT;
try {
for (int i = 0; i < nbIterations; i++) {
String fictiousClassloaderJAR = "file:" + i + ".jar";
URL[] fictiousClassloaderURL = new URL[] { new URL(fictiousClassloaderJAR) };
// Create a new classloader instance
URLClassLoader newClassLoader = new URLClassLoader(fictiousClassloaderURL);
// Create a new Proxy instance
ClassA t = (ClassA) Proxy.newProxyInstance(newClassLoader,
new Class<?>[] { ClassA.class },
new ClassAInvocationHandler(new ClassAImpl()));
// Add the new Proxy instance to the leaking HashMap
classLeakingMap.put(fictiousClassloaderJAR, t);
}
}
catch (Throwable any) {
System.out.println("ERROR: " + any);
}
System.out.println("Done!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment