Created
May 16, 2012 23:01
-
-
Save anonymous/2714735 to your computer and use it in GitHub Desktop.
Test harness for dynamically loading modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContainerTest | |
{ | |
@Test | |
public void testDefaultClassLoader() throws Exception | |
{ | |
test(new IClassLoaderFactory() | |
{ | |
public ClassLoader factory() throws MalformedURLException | |
{ | |
// load classes using the default delegation strategy | |
return new URLClassLoader(buildClassPath()); | |
} | |
}); | |
} | |
protected void test(IClassLoaderFactory factory) throws Exception | |
{ | |
String moduleClass = getModuleClass(); | |
IContainer container = buildContainer(); | |
System.gc(); | |
long memStart = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | |
for (int i=0; i<100; i++) | |
{ | |
ClassLoader moduleLoader = factory.factory(); | |
// load the module | |
container.loadModule(moduleLoader, moduleClass); | |
// now free the module | |
container.clearModules(); | |
System.gc(); | |
} | |
System.gc(); | |
long memEnd = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); | |
long kbLeaked = memEnd > memStart ? (memEnd - memStart) >> 10 : 0; | |
System.out.println("Leaked " + kbLeaked + "KB"); | |
assertTrue(kbLeaked < 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment