17

I'm writing a function that will call itself up to about 5000 times. Ofcourse, I get a StackOverflowError. Is there any way that I can rewrite this code in a fairly simple way?:

void checkBlocks(Block b, int amm) {

    //Stuff that might issue a return call

    Block blockDown = (Block) b.getRelative(BlockFace.DOWN);
    if (condition) 
        checkBlocks(blockDown, amm);


    Block blockUp = (Block) b.getRelative(BlockFace.UP);
    if (condition) 
        checkBlocks(blockUp, amm);

    //Same code 4 more times for each side

}

By the way, what is the limitation of how deep we may call the functions?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42

6 Answers6

21

Use an explicit stack of objects and a loop, rather than the call stack and recursion:

void checkBlocks(Block b, int amm) {
  Stack<Block> blocks = new Stack<Block>();
  blocks.push(b);
  while (!blocks.isEmpty()) {
    b = blocks.pop();
    Block blockDown = (Block) b.getRelative(BlockFace.DOWN);
    if (condition)
      blocks.push(block);
    Block blockUp = (Block) b.getRelative(BlockFace.UP);
    if (condition) 
      blocks.push(block);
  }
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Richante
  • 4,353
  • 19
  • 23
7

default stack size in java is 512kb. if you exceed that program will terminate throwing StackOverflowException

you can increase the stack size by passing a JVM argument : -Xss1024k

now stack size is 1024kb. you may give higher value based on your environment

I don't think we can programmatically change this

Anish Dasappan
  • 415
  • 2
  • 9
0

You can increase the stack size by using -Xss4m.

Sandro
  • 1,266
  • 2
  • 13
  • 25
  • But what if the board size just keeps increasing? I think he realized this and did not ask how to increase the stack size, but rather how to refactor the code! – barsju Apr 09 '12 at 12:59
0

You may put your "Block"s into a queue/stack and iterate as long as Blocks are available.

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
0

It's obvious that you get StackOverflow with such branching factor of your recursion. In other languages it can be achieved by Tail Call Optimization. But I suppose your problem needs another way to solve.

Ideally, you perform some check on Block. Maybe you can obtain list of all blocks and check each of them iteratively?

mishadoff
  • 10,719
  • 2
  • 33
  • 55
0

In most cases recursion is used in a wrong way. You shouldn't get a stack over flow exception. Your method has no return type/value. How do you ensure your initial Block b is valid?

If you are using recursion, answer yourself the following question:

  • what is my recursion anchor (when do i stop with recursion)
  • what is my recursion step (how do I reduce my number of calculations)

Example:

  • n! => n*n-1!

my recursion anchor is n == 2 (result is 2), so I can calculate all results beginnging from this anchor.

my recursion step is n-1 (so each step I get closer to the solution (and in this fact to my recursion anchor))

zip
  • 579
  • 1
  • 3
  • 16