Home > Archives > JVM OutOfMemoryError

JVM OutOfMemoryError

Publish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.ArrayList;
import java.util.List;

public class Test {
    static class OOMObject {

    }
    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<OOMObject>();
        while(true){
            list.add(new OOMObject());
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
baoguo@MacBook-Pro java % java -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError Test
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid9597.hprof ...
Heap dump file created [27555334 bytes in 0.085 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at Test.main(Test.java:11)
  

  
baoguo@MacBook-Pro java % jhat -port 7401 -J-Xmx4G java_pid9597.hprof
baoguo@MacBook-Pro ~ % jconsole
  
  
  1. 可以使用JProfile去查看hprof文件,然后发现问题。 //JProfile可以单独开个专题来研究
  2. 可以使用Jhat去跟踪

http://127.0.0.1:7401 就可以访问

Instance Counts for All Classes (including platform)

Instance Counts for All Classes (including platform)810326 instances of class Test$OOMObject

810326 instances of class Test$OOMObject 949 instances of [class C 937 instances of class java.lang.String 502 instances of [class Ljava.lang.Object;

可以查看到哪个对象占用的Heap比较多。

引用

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo