内存溢出(Out Of Memory)是指JVM可用内存不足。JVM运行需要使用的内存超出最大可用值,会导致JVM出现异常。常见的OOM有以下几种:
栈溢出
堆溢出
方法区溢出
本机直接内存溢出
1.栈溢出
/**
* 栈溢出
*/
public class StackOverFlow {
static int count;
//方法运行一次,往虚拟机栈压入一个栈帧
private static void king() {
count++;
king();
}
public static void main(String[] args) throws Throwable {
try {
king();
} catch (Throwable throwable) {
System.out.println("count:"+count);
throwable.printStackTrace();
}
}
}
在IDEA开发工具中,设置栈的大小1MB:
-Xss1m