Search code examples
scalastack-overflow

Is there any way to configure stack memory size with just the code of Scala?


I am working on an algorithm problem. I need to traverse a tree with 30000 nodes in pre-order.

With my experiments, I found when the recursion level is bigger than 10000, it will result in stack overflow. We know non-recursion version of pre-order traversing of a general tree is not that easy to implement, so I am trying to make the stack size bigger.

Since it is an online judge, I can't configure the stack size directly. I know there is a way to write some #prgram instructions in C++ to change the stack size, so I want to if there exists something similar to that in Scala.


Solution

  • If it's scala it's almost certainly running on some sort of JVM. The stack size is one of the properties of JVM that remains constant during it's lifetime. So I guess you're out of luck using just recursion.

    Have a look at: programatically setting max java heap size

    If it's tail recursive you could convert it to an iteration, but I guess you'd know that already.