虚拟机栈和本地方法栈溢出:
由于在HotSpot虚拟机中并不区分虚拟机栈和本地方法栈,因此,对于HotSpot来说,虽然-Xoss参数(设置本地方法栈大小)存在,但实际上是无效的,栈容量只由-Xss参数设定。关于虚拟机栈和本地方法栈,在Java虚拟机规范中描述了两种异常:
如果线程请求的栈深度大于虚拟机所允许的最大深度,将抛出StackOverflowError异常。
如果虚拟机在扩展栈时无法申请到足够的内存空间,则抛出OutOfMemoryError异常。
public class StackTest {
/**
* -Xss128k
* @param args
*/
public static void main(String[] args) {
TT tt = new TT();
tt.stackLeak();
}
static class TT {
private int stackLength = 1;
public void stackLeak() {
stackLength++;
stackLeak();
}
}
}
-Xss: 栈空间大小。
运行结果: