expoter
expoter

Reputation: 1640

Why class can not be loaded by URLClassLoader?

I want to simulate metaspace OOM. I plan to load class ClassA by different URLClassLoader, here is the code:

package classloader;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

class ClassA {
    public void method(String input){}
}

public class ClassMetadataLeakSimulator {
    private final static int NB_ITERATIONS_DEFAULT = 50000;

    public static void main(String[] args) {
        System.out.println("Class metadata leak simulator");
        int nbIterations = (args != null && args.length == 1) ? Integer.parseInt(args[0]) : NB_ITERATIONS_DEFAULT;
        try {
            List<ClassLoader> list = new ArrayList<>();
            URL url = new File(".").toURI().toURL();
            URL[] urls = new URL[]{url};
            System.out.println(url);
            for (int i = 0; i < nbIterations; i++) {
                URLClassLoader newClassLoader = new URLClassLoader(urls);
                list.add(newClassLoader);
                newClassLoader.loadClass("classloader.ClassA");
            }
        }
        catch (Throwable any) {
            System.out.println("ERROR: " + any);
        }
        System.out.println("Done!");
    }
} 

But, It's odd that the loaded classes number stop increasing when it reach 1437 which is shown in jvisualvm, and metaspace size used was low, even though the for loop has run millions of times. It seems that ClassA wasn't loaded by every new URLClassLoader instance. Why?

Upvotes: 1

Views: 476

Answers (1)

vsminkov
vsminkov

Reputation: 11250

The problem is that URLClassLoader is parented by system classloader. And on load it will try to get class from parent classloader at first. So class will be loaded only once and you'll not get metaspace OOM.

You need an isolated classloader to do this job. The other option is to generate classes on fly with cglib or asm libraries.

Also loadClass accepts not canonical but binary class names.

Upvotes: 1

Related Questions