为了账号安全,请及时绑定邮箱和手机立即绑定

如何模拟 OutOfMemoryError: Metaspace

如何模拟 OutOfMemoryError: Metaspace

斯蒂芬大帝 2021-10-28 10:44:28
我想知道如何自己导致 OutOfMemoryError: Metaspace 异常?是否可以加载很多类并让它们在内存中停留很长时间。
查看完整描述

3 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

要创建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());

           }

    }

}


查看完整回答
反对 回复 2021-10-28
  • 3 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信