標籤:
package com.xiaoysec.test;import java.util.ArrayList;import java.util.List;/** *VM Args:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 * @author xiaoysec * */public class HeapOOM { static class OOMObject{ //定義一個靜態內部類 } public static void main(String[] args){ List<OOMObject> list = new ArrayList<HeapOOM.OOMObject>(); //int i=0; while(true){ list.add(new OOMObject()); //System.out.println(i); //i++; } }}
代碼是相當簡單的,就是一個HeapOOM類進行HeapOutofMemory測試,在該類裡面有一個靜態內部類OOMObject
主要的思想就是在main方法裡面用一個死迴圈不停地建立對象我們知道java對象是在java堆裡面的當堆記憶體不足的時候虛擬機器會進行記憶體回收,但是當虛擬機器發現這些對象還是有用的不能被回收時,就擴充堆空間,但在本例當中有一個while(true)也就是說即使擴充了空間也是沒用的!
值得一提的是我們需要在VM arguments中設定 -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8也就是把堆的最大最小值設定為相同的20M以避免堆進行自動擴充
下面就是運行結果:
[GC (Allocation Failure) [PSYoungGen: 7882K->1000K(9216K)] 7882K->5433K(19456K), 0.0100414 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) --[PSYoungGen: 9192K->9192K(9216K)] 13625K->19424K(19456K), 0.0155543 secs] [Times: user=0.05 sys=0.00, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 9192K->0K(9216K)] [ParOldGen: 10232K->10051K(10240K)] 19424K->10051K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.2144901 secs] [Times: user=0.36 sys=0.00, real=0.21 secs]
[Full GC (Ergonomics) [PSYoungGen: 7635K->8076K(9216K)] [ParOldGen: 10051K->7940K(10240K)] 17687K->16017K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.1848934 secs] [Times: user=0.37 sys=0.00, real=0.18 secs]
[Full GC (Allocation Failure) [PSYoungGen: 8076K->8076K(9216K)] [ParOldGen: 7940K->7940K(10240K)] 16017K->16017K(19456K), [Metaspace: 2756K->2756K(1056768K)], 0.1011278 secs] [Times: user=0.33 sys=0.00, real=0.10 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.xiaoysec.test.HeapOOM.main(HeapOOM.java:20)
[Full GC (Ergonomics) [PSYoungGen: 8192K->0K(9216K)] [ParOldGen: 7940K->656K(10240K)] 16132K->656K(19456K), [Metaspace: 2781K->2781K(1056768K)], 0.0056287 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 9216K, used 82K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 1% used [0x00000000ff600000,0x00000000ff614938,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
ParOldGen total 10240K, used 656K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 6% used [0x00000000fec00000,0x00000000feca4310,0x00000000ff600000)
Metaspace used 2787K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 307K, capacity 386K, committed 512K, reserved 1048576K
java堆溢出的小栗子