首页 > JVM > 一个堆溢出的demo

一个堆溢出的demo

作者:bin

为了更快看到结果,我们将JVM的启动参数配置为:

-XX:+HeapDumpOnOutOfMemoryError -Xmx10m -Xms10m


相关代码

public class Part9 {

    public static void main(String[] args) throws Exception{
        List<String> list = new ArrayList<>();
        while(true){
            list.add(new String("test"));
            System.out.println(list.size() 
               + "--" + Runtime.getRuntime().totalMemory()/(1024*1024)+"M");
        }
    }
}

输出结果:

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid22058.hprof ...
Heap dump file created [14788857 bytes in 0.096 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)

然后我们可以堆java_pid22058.hprof,进行分析,可以参考MAT:http://zengbingo.com/p/1570.html

您必须 [ 登录 ] 才能发表留言!