How to create java.lang.OutOfMemoryError: Metaspace?

pavloN :

My goal is to simulate a situation in which I can receive this error. I had tried it How to simulate OutOfMemoryError: Metaspace but it didn't help.

I set in config: -XX:MaxMetaspaceSize=50m

Have tried it:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; ; i++) {
            Thread thread = new Thread(new A());
            thread.start();
        }
    }
}

and it: A and B POJO classes

public class Main {
    static Map<A,B> map = new LinkedHashMap<>();
    public static void main(String[] args) {
        while (true){
            A a = new A();
            B b = new B();
           map.put(a,b);
        }
    }
}
pero_hero :

as you mentioned you want a java.lang.OutOfMemoryError: Metaspace to be thrown. In order to achieve this, you have to load a lot of different classes. This can be accomplished by utilizing javaassist:

   static javassist.ClassPool cp = javassist.ClassPool.getDefault();

    public static void main(String[] args) throws Exception{
        for (int i = 0; ; i++) {
            Class c = cp.makeClass("mypackage.Myclass" + i).toClass();
        }
    }

with following metaspace settings:

-XX:MaxMetaspaceSize=10m
-XX:MetaspaceSize=2M
-XX:MaxMetaspaceFreeRatio=1
-XX:MaxMetaspaceExpansion=1K
-XX:MinMetaspaceFreeRatio=1
-XX:InitialBootClassLoaderMetaspaceSize=2M

without javaassist, it is possible to generate java.lang.OutOfMemoryEror: Metaspace keeping the same metaspace options as follows:

            String clazzBase64 = "yv66vgAAADcADAEAEm15cGFja2FnZS9NeWNsYXNzMAcAAQEAEGphdmEvbGFuZy9PYmplY3QHAAMBAApTb3VyY2VGaWxlAQANTXljbGFzczAuamF2YQEABjxpbml0PgEAAygpVgwABwAICgAEAAkBAARDb2RlACEAAgAEAAAAAAABAAEABwAIAAEACwAAABEAAQABAAAABSq3AAqxAAAAAAABAAUAAAACAAY=";
            byte[] compiledClazz = Base64.getDecoder().decode(clazzBase64);
            int len = Integer.valueOf(compiledClazz[12]);

            MyClassLoader myClassLoader = new MyClassLoader(Thread.currentThread().getContextClassLoader());

            for (int i = 0; ; i++) {
                byte[] bytes = String.valueOf(i).getBytes();
                byte[] bytecode = new byte[compiledClazz.length + bytes.length - 1];
                System.arraycopy(compiledClazz, 0, bytecode, 0, 30);
                bytecode[12] = (byte)( len + bytes.length - 1 & 0xFF);

                System.arraycopy(bytes, 0, bytecode, 30, bytes.length);
                System.arraycopy(compiledClazz, 31, bytecode, 30 + bytes.length, compiledClazz.length - 31);

                String classname ="mypackage.Myclass" + i;
                Class c = myClassLoader.getClass(classname, bytecode);
            }

            public static class MyClassLoader extends ClassLoader {
               public MyClassLoader(ClassLoader parent) {
                  super(parent);
               }

               public Class<?> getClass(String name, byte[] code) {
                  return defineClass(name, code, 0, code.length);
               }
            }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409052&siteId=1