java - How to create java.lang.OutOfMemoryError: Metaspace?
问题描述
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);
}
}
}
解决方案
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);
}
}
推荐阅读
- node.js - 将 NodeJS 用于社交媒体应用程序的隐私选项
- c - 在一个线程中读取/写入文件描述符,在另一个线程中关闭?
- css - 有没有办法根据孩子的状态将css应用于父元素?
- java - Maven 构建成功,但我的测试用例没有在 eclipse 中运行
- javascript - 在“it”方法中手动调用 afterEach 方法
- nginx - 从一个 nginx 服务器提供多个单页应用程序的多个版本?
- javascript - 如何使用`addEventListener`中的模糊事件获取所有表单输入值?
- react-native - React Native Navigation - 尝试根据屏幕更改抽屉
- c# - 如何进行正确的依赖注入
- azure-cosmosdb - 带有 SQL API 的 CosmosDB - "_rid" 字段的增量顺序