简体   繁体   中英

Java: Stackoverflow in finite recursion

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

Yes, use the -Xss parameter. eg:

java -Xss4m Blah

sets the stack-size to 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. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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