一、虚拟机栈?
Java虚拟机栈(Java Virtual Machine Stack)也是线程私有的,它的生命周期与线程相同。虚拟机栈描述的是Java方法执行的线程内存模型:每个方法被执行的时候,Java虚拟机都会同步创建一个栈帧(Stack Frame)用于存储局部变量表、操作数栈、动态连接、方法出口等信息。每一个方法被调用直至执行完毕的过程,就对应着一个栈帧在虚拟机栈中从入栈到出栈的过程。
二、文档介绍
-Xss size
Sets the thread stack size (in bytes). Append the letter k or K to indicate KB, m or M to indicate MB, and g or G to indicate GB. The default value depends on the platform:
Linux/x64 (64-bit): 1024 KB
macOS (64-bit): 1024 KB
Oracle Solaris/x64 (64-bit): 1024 KB
Windows: The default value depends on virtual memory
The following examples set the thread stack size to 1024 KB in different units:
-Xss1m
-Xss1024k
-Xss1048576
三、在idea中修改
run -> edit Configuration ,修改vm option的参数
根据
oracle给的文档
设置参数格式
四、验证
写一个递归的死循环
public class StackErrorTest {
private static int count = 1;
public static void d(){
System.out.println(count++);
d();
}
public static void main(String[] args) {
StackErrorTest.d();
}
}
默认情况:递归调用9859次就报StackOverFlowError了。
修改虚拟机栈内存为256k:约调用2423次。猜测本机的虚拟机栈内存大小约为1M
在《Java虚拟机规范》中,对这个内存区域规定了两类异常状况:如果线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowError异常;如果Java虚拟机栈容量可以动态扩展,当栈扩展时无法申请到足够的内存会抛出OutOfMemoryError异常。