java堆溢出演示-OutOfMemoryError

jvm 参数

-Xms20m -Xmx20m  -XX:+HeapDumpOnOutOfMemoryError

代码示例


import java.util.ArrayList;
import java.util.List;

public class HeapOOM {

	static class OOMObject{
		
	}
	
	public static void main(String[] args) {
		List<OOMObject> list = new ArrayList<>();
		while(true) {
			list.add(new OOMObject());
		}
	}
}

结果

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid17808.hprof ...
Heap dump file created [27978623 bytes in 0.066 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 myrabbitmq.HeapOOM.main(HeapOOM.java:15)