1 栈溢出
package com.epro.jvm;
public class StackOverflowError {
private int i = 0;
public void a() {
System.out.println(i++);
a();
}
public static void main(String[] args) {
StackOverflowError j = new StackOverflowError();
j.a();
}
}
2 堆溢出
package com.epro.jvm;
import java.util.ArrayList;
import java.util.List;
public class HeapOOM {
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
try {
while(true) {
aList.add("asdf1234");
}
}catch(Throwable e) {
System.out.println(aList.size());
e.printStackTrace();
}
}
}
3 方法区溢出
方法区溢出需要配置参数:右键MethodOutOf类 -> Debug As -> Debug Configurations -> Arguments,在VM arguments的地方配置-XX:MaxMetaspaceSize=1m(jdk1.8以上的版本)
jdk 1.7 和1.8 设置方法区溢出的参数是不一样的
https://blog.csdn.net/qq_35730039/article/details/80736316
package com.epro.jvm;
public class MethodOutOf {
public static void main(String[] args) {
int i=1;
while(i< Integer.MAX_VALUE){
System.out.println("当前正在进行方法区溢出测试..." +i);
i++;
}
}
}