Programmatically fill the JVM Permanent Generation (PermGen) memory region

只谈情不闲聊 提交于 2019-12-03 06:13:51

问题


I need to test some JMX monitoring scripts I have developed, In particular I would like to verify that my monitoring of the PermGen region is working. So in order to test this I would like to be able to run a bit of code that loads a significant number of classes in order to consume PermGen.

My current plan is to write a script to generate prefix(1..n).java compile them and then on cue run:

for( int i=1 ; i < n ; i ++){
    Class.forName("com.mypackage.prefix"+i);
}

Is there a more elegant solution to achieve this?


回答1:


OK, so looks like String.intern() will do the trick. Here is one implementation I found. Credits goes to Gareth as well:

public static void main(String[] args) throws ClassNotFoundException {
    int i = 0;
    StringBuilder sb = new StringBuilder("a");
    for (i = 0; i < 20; i++) {
        sb.append(sb.toString());
    }
    System.err.println(sb.length());
    i = 0;
    Set<String> strings = new HashSet<String>();
    while (true) {
        strings.add(sb.append(i++).toString().intern());
        System.err.println(i);
    }
}


来源:https://stackoverflow.com/questions/9802626/programmatically-fill-the-jvm-permanent-generation-permgen-memory-region

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!