一、堆溢出
import java.util.ArrayList;
import java.util.List;
public class Demo_071202 {
public static void main(String[] args) {
List<Test> list=new ArrayList<>();
while (true){
list.add(new Test());
}
}
static class Test{}
}
设置JVM初始堆空间和最大堆空间,堆溢出生成快照。
-Xms20M -Xmx20M -XX:+HeapDumpOnOutOfMemoryError
设置虚拟机参数
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid21336.hprof ...
Heap dump file created [22149788 bytes in 0.062 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3720)
at java.base/java.util.Arrays.copyOf(Arrays.java:3689)
at java.base/java.util.ArrayList.grow(ArrayList.java:238)
at java.base/java.util.ArrayList.grow(ArrayList.java:243)
at java.base/java.util.ArrayList.add(ArrayList.java:486)
at java.base/java.util.ArrayList.add(ArrayList.java:499)
at com.demo.controller.Demo_071202.main(Demo_071202.java:10)
二、栈溢出
public class Demo_071203 {
public static void main(String[] args) {
test();
}
public static void test(){
test();
}
}
Exception in thread "main" java.lang.StackOverflowError
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)
at com.demo.controller.Demo_071203.test(Demo_071203.java:8)