How to "close" ClassLoader?

I have a case where I need to create many cool downloaders in my application in order to temporarily make some code visible while user scripts are running. I use URLClassLoader for this and it works very well.

When the script exits, I want to "unload" or "close" the class loader to free up resources.

Is it enough to set the link to the class loader to null ? I am particularly interested in whether I can ultimately work with file descriptors because the extra classes are in JAR files.

PS: Should work with Java 5 and above. Yes I know...

+8
java urlclassloader
source share
6 answers

When all classes loaded by the class loader no longer have links, and all links to the class loader have been deleted, the class loader and loaded classes will collect garbage as a group.

Note that this depends on whether the JVM attribute is set, which unloads unwritten classes. It is installed by default in most environments, but may not be in some built-in cases.

[Note that this is not a trivial question to remove class references. Any other class that refers to it by name will, of course, prevent deletion. Therefore, the class must be loaded using ClassLoader.findClass or something similar.]

+4
source share

A little late, but I hope it will be useful for those who come to this Question later (like me).

With Java 7, the close () method was added to the URLClassLoader, which is exactly what the OP was asking for.

EDIT (thanks to @Hot Licks) : OK, so this is not exactly what the OP asked for. It does not free all resources or make resources and the collector available. It just prevents loading more resources with the class loader. However, it closes the jar file that was loaded using URLClassLoader .

+5
source share

If you no longer have classes (and objects) loaded from this class loader, and if you do not reference this class loader, it will be automatically handled by the garbage collector.

+3
source share

If you cannot use Java7 and the close () method, use reflection to close all open class loader JAR archives, for example:

 public void close() { try { Class clazz = java.net.URLClassLoader.class; java.lang.reflect.Field ucp = clazz.getDeclaredField("ucp"); ucp.setAccessible(true); Object sun_misc_URLClassPath = ucp.get(this); java.lang.reflect.Field loaders = sun_misc_URLClassPath.getClass().getDeclaredField("loaders"); loaders.setAccessible(true); Object java_util_Collection = loaders.get(sun_misc_URLClassPath); for (Object sun_misc_URLClassPath_JarLoader : ((java.util.Collection) java_util_Collection).toArray()) { try { java.lang.reflect.Field loader = sun_misc_URLClassPath_JarLoader.getClass().getDeclaredField("jar"); loader.setAccessible(true); Object java_util_jar_JarFile = loader.get(sun_misc_URLClassPath_JarLoader); ((java.util.jar.JarFile) java_util_jar_JarFile).close(); } catch (Throwable t) { // if we got this far, this is probably not a JAR loader so skip it } } } catch (Throwable t) { // probably not a SUN VM } return; } 
+3
source share

There are no close () methods in the URL loader method or in any of its parent classes, so you're out of luck.

Should I handle GC?

+1
source share

I extended URLClassLoader and applied the close method based on Java 7s. I wanted to develop my IRC bot on my iPad 2, so I did what I needed. Now my plugin system is stable on Java 6 and 7, cheers.

0
source share

All Articles