idk321's blog

By idk321, history, 6 weeks ago, In English

If I run a recursive (Java) program on my machine, stack overflow usually occures before reaching depth of 10000 (there is no tail recursion since Java does not make it available unfortunately). Yet on Codeforces my recursive programs on trees work up to 100000 or more (one such program I found is Does anyone have an idea why is it like this and how can I make it so this would also work on my machine or on other less advanced online judges?

  • Vote: I like it
  • +30
  • Vote: I do not like it

6 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

You can increase a stack size with -Xss flag, so you should you run your program like java -Xss500m Main. You can find more in this question on StackOverflow

6 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I think there's stack overflow in codeforces too. I don't remember exactly which problem was that, but I have faced issues when I try to run a recursive function that can go deep upto 10^6 levels.

Edit : Nevermind, I just rechecked all my MLE submissions and all of them were stupid issues not related to stack-overflow. :P

6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

It is possible to get stack overflow 92174392.

Consider adding something like

	public static void solve() {
		//code here
	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(null, () -> solve(), "", 1<<28);

which sets the stack size to 1<<28 ($$$2^{28}$$$). Note that this doesn't allow you to do recursion up to $$$2^{28}$$$ levels (you'll get MLE), but it basically overcomes stack overflow. Locally, add the -Xss flag when running. -Xss 512m is usually enough.