7

I was playing around with classLoaders in Java and noticed a strange thing. If a classLoader loads a class from a jar, this jar is locked indefinitely even if you unreference your classLoader.

In the below example, the jar contains a class called HelloWorld. What I do is try to load the class contained in the jar via a classLoader which adds the jar dynamically. If you set skip to true and do not call Class.forName, you can delete the jar but if you do not skip and even if you unreference the classLoader (classLoader = null), the jar cannot be deleted until the JVM exits.

Why is that?

PS: I am using java 6 and the code is very verbose for testing purposes

package loader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class TestClassLoader {

    private URLClassLoader classLoader;

    public TestClassLoader() throws MalformedURLException, IOException {
        System.out.println("Copying jar");
        if (copyJar()) {
            System.out.println("Copying SUCCESS");
            performFirstCheck();
        } else {
            System.out.println("Copying FAILED");
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("Test started");
        TestClassLoader testClassLoader = new TestClassLoader();
        System.out.println("Bye!");
    }

    public void performFirstCheck() throws IOException {
        System.out.println("Checking class HelloWorld does not exist");
        if (!checkClassFound(TestClassLoader.class.getClassLoader(), false)) {
            System.out.println("Deleting jar");
            deleteJar();
            System.out.println("First Check SUCCESS");
            performSecondCheck();
        } else {
            System.out.println("First Check FAILED");
        }
    }

    private void performSecondCheck() throws IOException {
        System.out.println("Copying jar");
        if (copyJar()) {
            System.out.println("Copying SUCCESS");
            createClassLoaderAndCheck();
        } else {
            System.out.println("Copying FAILED");
        }
    }

    private void createClassLoaderAndCheck() throws MalformedURLException {
        System.out.println("Creating classLoader");
        createClassLoader();
        System.out.println("Checking class HelloWorld exist");
        if (checkClassFound(classLoader, true)) {
            System.out.println("Second Check SUCCESS");
                    classLoader = null;
            System.out.println("Deleting jar");
            if (deleteJar()) {
                System.out.println("Deleting SUCCESS");
            } else {
                System.out.println("Deleting FAILED");
            }
        } else {
            System.out.println("Second Check FAILED");
        }
    }

    public void createClassLoader() throws MalformedURLException {
        URL[] urls = new URL[1];
        File classFile = new File("C:\\Users\\Adel\\Desktop\\classes.jar");
        urls[0] = classFile.toURI().toURL();
        classLoader = new URLClassLoader(urls);
    }

    public boolean checkClassFound(ClassLoader classLoader, boolean skip) {
        if (skip) {
            System.out.println("Skiping class loading");
            return true;
        } else {
            try {
                Class.forName("HelloWorld", true, classLoader);
                return true;
            } catch (ClassNotFoundException e) {
                return false;
            }
        }
    }

    public URLClassLoader getClassLoader() {
        return classLoader;
    }

    public boolean copyJar() throws IOException {
        File sourceJar = new File("C:\\Users\\Adel\\Desktop\\Folder\\classes.jar");
        File destJar = new File("C:\\Users\\Adel\\Desktop\\classes.jar");
        if (destJar.exists()) {
            return false;
        } else {
            FileInputStream finput = new FileInputStream(sourceJar);
            FileOutputStream foutput = new FileOutputStream(destJar);
            byte[] buf = new byte[1024];
            int len;
            while ((len = finput.read(buf)) > 0) {
                foutput.write(buf, 0, len);
            }
            finput.close();
            foutput.close();
            return true;
        }
    }

    public boolean deleteJar() {
        File destJar = new File("C:\\Users\\Adel\\Desktop\\classes.jar");
        return destJar.delete();
    }

}
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89

2 Answers2

10

I have found an answer and a workaround.

Based on this article and this amazing related article, it is a bad habit to use Class.forName(className, true, classLoader) because it keeps the class cached in the memory indefinitely.

The solution was to use classLoader.loadClass(clasName) instead, then once finished, unreference the classLoader and call the garbage collector using:

classLoader = null;
System.gc();

Hope this helps others! :)

Background Info:

My project was a complexe one: we had a GWT server acting as a RMI client to another server. So to create instances, GWT needed to download the classes from the server and load them. Later, GWT would resend instance to the server to persist them in database using Hibernate. In order to support hot deployment, we opted for dynamically class loading where a user would upload a jar and notifies the server who would load the classes from it and present them as available to GWT server

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • Note that this only works if *all* classes loaded by this class loader and *all* instances of any of these classes are also unreferenced. As long as there is even one instance of such a class, the class loader will be kept in memory (because you can call `instance.getClass().getClassLoader()`). – Philipp Wendler Jun 30 '12 at 10:57
  • Excellent, altough all instances and classes needs to be derefernced - and they might end up in PermGen. I think one should note that in Java 7 we have the `URLClassLoader#close()` - which should be used. I have vague nightmarish memories from traversing classpaths and loading files into memory to load the classes from ... never really actually solved the problems. Classloaders are strange animals. – esej Jun 30 '12 at 10:58
  • @esej You probably mean creating a subclass of ClassLoader, manually opening the file and reading its content into a byte array and then calling `ClassLoader#defineClass(String name, byte[] b, int off, int len)`? This should indeed work. – Philipp Wendler Jun 30 '12 at 11:05
  • Yepp. That way, or a close variant, is how it is done in most middlewars I've looked at. It can get quite complicated :p – esej Jun 30 '12 at 11:07
3

In Java 7 URLClassLoader has a #close() method that fixes this.

Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
  • I mentioned I am using java 6 and this cannot be changed now :D – Adel Boutros Jun 30 '12 at 10:26
  • Java 6 is EoL really soon. You can try removing all references to the class loader and forcing a full GC _and_ running finalization. But this is terrible. Or you can write your own jar classloader that has a close method. – Philippe Marschall Jun 30 '12 at 10:32
  • 1
    I already found a solution using java 6, care to check my answer and share you opinion? – Adel Boutros Jun 30 '12 at 10:37