简体   繁体   English

Java:有限递归中的Stackoverflow

[英]Java: Stackoverflow in finite recursion

I wrote a javaCC parser for some propositional logic expressions. 我为一些命题逻辑表达式编写了一个javaCC解析器。 The expressions can get pretty long, 30k many characters. 表达式可以很长,最多30k个字符。

When I parse such big expressions, I get a stack-overflow exception. 当我解析这么大的表达式时,我得到了堆栈溢出异常。

Is there maybe some VM parameter which determines the stack size? 是否可能有一些VM参数确定堆栈大小?

Or what would you do in such cases? 或者在这种情况下您会怎么做?

Thanks 谢谢

Yes, use the -Xss parameter. 是的,使用-Xss参数。 eg: 例如:

java -Xss4m Blah

sets the stack-size to 4MB. 将堆栈大小设置为4MB。

While you can alter the stack size as per Oli's answer, I would suggest you try to look for an alternative approach which doesn't recurse so deeply. 虽然您可以按照Oli的回答更改堆栈大小,但我建议您尝试寻找一种替代方法,该方法不会产生那么深的影响。 For example, you might want to build a stack or queue of "results so far" or whatever, to mimic the recursion but using heap space instead of using stack space. 例如,您可能想构建一个堆栈或“到目前为止的结果”队列,以模拟递归,但要使用堆空间而不是堆栈空间。 Increasing the VM stack size always strikes me as a "solution" which is just delaying the problem a bit. 增加VM堆栈大小总是让我感到“解决方案”,这只是在延迟问题。

(It also means that if you end up with stack traces, they'll be monstrous... whereas if you use appropriate diagnostics for the stack of "things you're looking at" you can end up with data-driven diagnostics instead of ones based on stack frames.) (这也意味着,如果最终得到堆栈跟踪,它们将是可怕的……而如果您对“正在查看的事物”的堆栈使用适当的诊断程序,则可以得到数据驱动的诊断程序而不是那些基于堆栈框架。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM