我的目标是模拟一种可以接收到此错误的情况。
我试过了How to simulate OutOfMemoryError: Metaspace
但这没有帮助。

我在配置中设置:
-XX:MaxMetaspaceSize = 50m

试过了:

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

它:
A和B POJO类
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);
        }
    }
}

最佳答案

正如您提到的,您希望抛出java.lang.OutOfMemoryError: Metaspace
为了实现这一点,您必须加载许多不同的类。这可以通过使用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();
        }
    }

具有以下元空间设置:
-XX:MaxMetaspaceSize=10m
-XX:MetaspaceSize=2M
-XX:MaxMetaspaceFreeRatio=1
-XX:MaxMetaspaceExpansion=1K
-XX:MinMetaspaceFreeRatio=1
-XX:InitialBootClassLoaderMetaspaceSize=2M

如果没有javaassist,则可以生成保留相同元空间选项的java.lang.OutOfMemoryEror: Metaspace,如下所示:
            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);
               }
            }

09-28 05:24