使用Java程序时,可能要处理的一种更烦人的情况是StackOverFlowError,如果您有一个很好的可生产测试用例,那么关于使用堆栈大小或设置条件断点/某种痕迹 。
但是,如果您有一个测试案例可能一次失败100次,或者像我的案例一样在AWTMulticaster中出现竞争情况,那么您想改善诊断。 问题是默认情况下,在前1024个条目之后,VM不会在堆栈跟踪中包含任何元素。 (至少对于JDK 6而言)因此,如果运行以下简单示例:
package other;public class Overflow {public static final void call(double a, double b, double c, double d) {call(a,b,c,d);}public static void main(String[] args) {call(0,0,0,0);}}
在找到问题原因之前,输出将停止,这使得解决该问题非常困难。
> java other.Overflow
Exception in thread "main" java.lang.StackOverflowErrorat other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)[ lots of lines removed ]at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)
Process exited with exit code 1.
好消息是,此限制是启动虚拟机时可以调整的许多正式和非正式的内容之一。
> java -XX:MaxJavaStackTraceDepth=1000000 other.Overflow
Exception in thread "main" java.lang.StackOverflowErrorat other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)[ lots of lines removed ]at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.call(Overflow.java:7)at other.Overflow.main(Overflow.java:12)
Process exited with exit code 1.
请注意,堆栈的末尾现在包含了问题的根源,在尝试诊断问题时将非常有用。
您可能不想长期在生产服务器上设置此属性,因为我不确定它的影响。 查看C ++代码,似乎这只是一个最大值,不会影响大多数其他堆栈跟踪,因为它们在32个条目的段中的链接列表中分配。
参考:在Gerard Davison的博客博客中,从我们的JCG合作伙伴 Gerard Davison 的尾部捕获了StackOverFlowError 。
翻译自: https://www.javacodegeeks.com/2012/07/catch-stackoverflowerror-by-its-tail.html