Java: Stackoverflow in finite recursion

2019-09-09 06:43发布

I wrote a javaCC parser for some propositional logic expressions. The expressions can get pretty long, 30k many characters.

When I parse such big expressions, I get a stack-overflow exception.

Is there maybe some VM parameter which determines the stack size?

Or what would you do in such cases?

Thanks

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-09 07:31

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. 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.

(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.)

查看更多
▲ chillily
3楼-- · 2019-09-09 07:34

Yes, use the -Xss parameter. e.g.:

java -Xss4m Blah

sets the stack-size to 4MB.

查看更多
登录 后发表回答