gpt4 book ai didi

java - 如何创建 java.lang.OutOfMemoryError : Metaspace?

转载 作者:行者123 更新时间:2023-12-01 14:27:48 26 4
gpt4 key购买 nike

我的目标是模拟我可以收到此错误的情况。我试过了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);
}
}

关于java - 如何创建 java.lang.OutOfMemoryError : Metaspace?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61017610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com