Skip to content

Instantly share code, notes, and snippets.

Created May 16, 2012 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2714735 to your computer and use it in GitHub Desktop.
Save anonymous/2714735 to your computer and use it in GitHub Desktop.
Test harness for dynamically loading modules
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