猿问
如何模拟 OutOfMemoryError: Metaspace
我想知道如何自己导致 OutOfMemoryError: Metaspace 异常?是否可以加载很多类并让它们在内存中停留很长时间。
斯蒂芬大帝
浏览 109
回答 3
3回答
噜噜哒
要创建OutOfMemoryError: Metaspace使用javassist.ClassPool库。下面的示例将创建Metaspace错误。import javassist.ClassPool;public class OutOfMemoryErrorMetaspace { //ClassPool objects hold all the CtClasses. static ClassPool classPool = ClassPool.getDefault(); public static void main(String[] args) throws Exception { for (int i = 0; i < 1000000; i++) { //makeClass method - Creates a new class (or interface) from the given class file. Class clas = classPool.makeClass( i + " outofmemory.OutOfMemoryErrorMetaspace ").toClass(); //Print name of class loaded System.out.println(clas.getName()); } }}
0
0
0
随时随地看视频
慕课网APP
相关分类
Java
我要回答