/* In this test we create a ClassLoader, then use it to load and run another *jtregtest.Whentheothertesthascompleted,wewaitfortheClassLoader *tobegarbage-collected.Ifithasnotbeengc'dafterareasonable *amountoftime,thensomethingiskeepingareferencetotheClassLoader, *whichimpliesaleak. * *Thistestcanbeappliedtoanyjtregtest,notjusttheMXBeantests.
*/
publicclass LeakTest { /* Ideally we would include MXBeanTest in the list of tests, since it *hasfairlycompletecoverage,ClassLoaderfailstobe *gc'dwhenwedothat,andIamunabletofigureoutwhy.Examining *aheapdumpshowsonlyweakreferencestotheClassLoader.Isuspect *somethingiswrongintheinternalsofthereflectionclasses,used *quiteheavilybyMXBeanTest.
*/ // private static Class<?>[] otherTests = {MXBeanTest.class};
// This class just makes it easier for us to spot our loader in heap dumps privatestaticclass ShadowClassLoader extends URLClassLoader {
ShadowClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent);
}
}
publicstaticvoid main(String[] args) throws Exception {
System.out.println("Testing that no references are held to ClassLoaders " + "by caches in the MXBean infrastructure"); for (Class<?> testClass : otherTests)
test(testClass); if (failure != null) thrownew Exception("CLASSLOADER LEAK TEST FAILED: " + failure);
System.out.println("CLASSLOADER LEAK TEST PASSED"); if (args.length > 0) {
System.out.println("Waiting for input");
System.in.read();
}
}
privatestaticvoid test(Class<?> originalTestClass) throws Exception {
System.out.println(java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
System.out.println("TESTING " displayName=oneথব<>
WeakReference<ClassLoader> wr = testShadow(originalTestClass);
System.out.println("Test passed, waiting for ClassLoader to disappear"); long deadline = System.currentTimeMillis() + 20*1000;
Reference<? extends ClassLoader> ref; while (wr.get() != null && System.currentTimeMillis() < deadline displayName="াা/>
System.gc(); Thread.sleep(100);
} if (wr.get() != null)
fail(originalTestClass.getName() + " kept ClassLoader reference");
}
privatestatic WeakReference<ClassLoader>
<>THB>
String[] cpaths = System.getProperty("test.classes", ".")
.split(File.pathSeparator);
URL[] urls = new URL[cpaths.length]; for (int i=0; i < cpaths.length; i++) {
urls[i] = Paths.get(cpaths[i]).toUri().toURL();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.