读书笔记01 关于OutOfMemoryError 堆空间的溢出

时间:2021-09-06 21:05:57

代码片段如下:

package com.gosaint.shiro; import java.util.ArrayList; import java.util.List; public class HeadOOM { static class OOMObject{ } public static void main(String[] args) { List<OOMObject> list=new ArrayList<OOMObject>(); while(true){ list.add(new OOMObject()); } } }

配置主方法运行参数:

-verbose:gc -Xms20m -Xmx20m -XX:+PrintGCDetails -XX:SurvivorRatio=8

异常如下:

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 com.gosaint.shiro.HeadOOM.main(HeadOOM.java:14)