问题
I am investigating memory leak in some JVM bsaed code (groovy running in a Java env).
I have a simple Java test class that I am using to investigate various behaviours around Groovy class loading, and I am running the following code (from plain Java):
GroovyClassLoader localGroovyClassLoader = new GroovyClassLoader();
Class clazz = localGroovyClassLoader.loadClass("com.test.Example", true, false, true);
Where Example is just a simple Groovy class (doesn't do anything particular as its just my dummy case).
Now, if immediately after the above code I log the following:
localGroovyClassLoader.getLoadedClasses().length
It logs a 0 - despite that I can verify that my loaded class is all good (clazz outputs that it is in fact Example).
The reason I want to use the getLoadedClasses() method is because I am dynamically reloading groovy on the fly, so when reloading I want to also clear the meta registry (as detailed here: https://stackoverflow.com/a/10484023/258813 ) - so was planning on using the following code:
for (Class c : localGroovyClassLoader.getLoadedClasses()){
GroovySystem.getMetaClassRegistry().removeMetaClass(c);
}
But obviously, if getLoadedClasses() returns 0 despite having just loaded a class I will not be able to run the above code.
Thanks
回答1:
Do you have the com.test.Example compiled class in the parent classloader? Calling getLoadedClasses only gives you the classes that this GCL loaded directly itself. If it was able to find a particular class by delegating to its parent loader then it doesn't need to load the class itself, and the class won't be included in getLoadedClasses.
来源:https://stackoverflow.com/questions/14461773/classes-loaded-by-groovyclassloader-not-listed