以编程方式填充JVM永久生成(PermGen)内存区域 [英] Programmatically fill the JVM Permanent Generation (PermGen) memory region
问题描述
我需要测试我开发的一些JMX监控脚本,特别是我想验证我对PermGen区域的监控是否正常。所以为了测试这个,我希望能够运行一些加载大量类的代码来消耗PermGen。
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.
我目前的计划是写一个脚本来生成前缀(1..n).java
编译它们然后在cue上运行:
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?
推荐答案
好的,所以看起来像String.intern()就可以了。这是我发现的一个实现。积分也归Gareth所有:
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);
}
}
这篇关于以编程方式填充JVM永久生成(PermGen)内存区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!