1、配置启动参数
VM arguments中加入如下内容:
-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
2、测试代码
public class TestOutOfMemoryError {
static class OOMObject{}
public static void main(String[] args) {
System.out.println("TestOutOfMemoryError");
List<OOMObject> list = new ArrayList<OOMObject>();
while(true) {
list.add(new OOMObject());
}
}
}
3、运行
TestOutOfMemoryError
[GC (Allocation Failure) [DefNew: 8036K->1024K(9216K), 0.0140061 secs] 8036K->4733K(19456K), 0.0375589 secs] [Times: user=0.02 sys=0.00, real=0.04 secs]
[GC (Allocation Failure) [DefNew: 9216K->1024K(9216K), 0.0176372 secs] 12925K->10815K(19456K), 0.0176841 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[GC (Allocation Failure) [DefNew: 9216K->9216K(9216K), 0.0000180 secs][Tenured: 9791K->5863K(10240K), 0.0464165 secs] 19007K->14432K(19456K), [Metaspace: 79K->79K(4480K)], 0.0468024 secs] [Times: user=0.05 sys=0.00, real=0.05 secs]
[Full GC (Allocation Failure) [Tenured: 5863K->5863K(10240K), 0.0340768 secs] 14755K->14755K(19456K), [Metaspace: 79K->79K(4480K)], 0.0341092 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
[Full GC (Allocation Failure) [Tenured: 5863K->5853K(10240K), 0.0377713 secs] 14755K->14745K(19456K), [Metaspace: 79K->79K(4480K)], 0.0378298 secs] [Times: user=0.03 sys=0.00, real=0.04 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:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at com.suibibk.test.TestOutOfMemoryError.main(TestOutOfMemoryError.java:12)
Heap
def new generation total 9216K, used 8902K [0x04a00000, 0x05400000, 0x05400000)
eden space 8192K, 100% used [0x04a00000, 0x05200000, 0x05200000)
from space 1024K, 69% used [0x05200000, 0x052b1aa8, 0x05300000)
to space 1024K, 0% used [0x05300000, 0x05300000, 0x05400000)
tenured generation total 10240K, used 5853K [0x05400000, 0x05e00000, 0x05e00000)
the space 10240K, 57% used [0x05400000, 0x059b75e8, 0x059b7600, 0x05e00000)
Metaspace used 80K, capacity 2242K, committed 2368K, reserved 4480K