JVM初体验

一、设计堆内存溢出异常:OutOfMemoryError:

1 public class Main {
2     public static void main(String[] args) {
3         List<Demo> list = new ArrayList<>();
4         while(true) {
5             list.add(new Demo());
6         }
7     }
8 }

JVM参数:-XX:+HeapOnOutOfMemoryError   :导出内存溢出的堆信息(hprof文件)

执行后报错:

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid15804.hprof ...
Heap dump file created [28024160 bytes in 0.082 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.grow(Unknown Source)
    at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
    at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
    at java.util.ArrayList.add(Unknown Source)
    at com.qlu.test1.Main.main(Main.java:10)

查看文件:文件保存在项目文件夹中,但是要查看其中内容,你需要借助Eclipse Memory Analyzer工具。

 

posted @ 2019-10-04 21:19  菜菜菜鸡  阅读(155)  评论(0编辑  收藏  举报