线程栈溢出监控与分析详解
虚拟机栈溢出:
实例:
public class MyTest2 {
private int length;
public int getLength() {
return length;
}
public void test(){
this.length++;
try {
Thread.sleep(300); //这里,为了让当前进程保持的时间长一点,方便利用jvisualvm观察
} catch (InterruptedException e) {
e.printStackTrace();
}
test();
}
public static void main(String[] args) {
MyTest2 myTest2 = new MyTest2();
try {
myTest2.test();
}catch (Throwable ex){
System.out.println(myTest2.length);
ex.printStackTrace();
}
}
}
/**
-Xms<size> 设置初始 Java 堆大小
-Xmx<size> 设置最大 Java 堆大小
-Xss<size> 设置 Java 线程堆栈大小
*/
(1)在VM options中设置:-Xss10k
运行结果:
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
The stack size specified is too small, Specify at least 108k //栈内存至少要设置108k
(1)在VM options中设置:-Xss109k //大于108k时
运行结果:
985
java.lang.StackOverflowError //堆栈溢出
at jvm.memory.MyTest2.test(MyTest2.java:11)
at jvm.memory.MyTest2.test(MyTest2.java:12)
......
利用jvisualvm查看线程信息: