手动触发OutOfMemoryError异常

今天手动触发了一个内存堆溢出异常,感觉不错,嘿嘿~~

程序如下:

package com.test;

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

public class HeapOOM {

static class OOM {

}

public static void main(String[] args) throws Exception {

List<OOM > list = new ArrayList<OOM >();

while(true){
list.add(new OOM ());
}
}
}

抛出异常如下:

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.ensureCapacity(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at com.jobmonitor.test.HeapOOM.main(HeapOOM.java:17)

你可能感兴趣的:(手动触发OutOfMemoryError异常)