How to avoid StackOverflowError ?

I’ve got a method which calls itself recursively. For a few thousand iterations it works fine but then it throws an java.lang.StackOverflowError ; it’s with Java 1.4.2_04-b05 and no special Java command switches on a Win2000 box.
How could I increase that stack size?

Also, when the error is being thrown, my try-catch(Throwable th) block won’t print an error stack, despite of my line th.printStackTrace(). For other errors this works. Looks like the stack’s so full that the error stack can’t be pushed on it…
However then I’ve to guess the code line where exactly the StackOverflowError occurs. Is there a way to always get a full printStackTrace() ?

maybe -Xss

But you should think about such deep recursions…

You can often “simulate” recursion with partially-recursive code.

One trick is to “checkpoint” the results, so that you recurse in “chunks” and your stack never gets too big.

If you have pseudo-code you could post, we could possibly suggest some other tricks (there’s others too)?

The -Xss switch doesn’t help unfortunately.

I’d happily use another way instead of recursion, but I can’t think of one. I’ve described what I basically try to do (finding all polygons in a smooth group) here: http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1080400827;start=7#7
… however since it’s not a pure Xith question I thought let’s ask here, too. :slight_smile:

+[quote]You can often “simulate” recursion with partially-recursive code.

One trick is to “checkpoint” the results, so that you recurse in “chunks” and your stack never gets too big.

If you have pseudo-code you could post, we could possibly suggest some other tricks (there’s others too)?
[/quote]
The checkpoint sounds well. Still I’m unsure how I could use it with my problem, but I’ll think about it… :slight_smile:

The task is to find for a 3d modell all adjacent polygons with a soft angle (between their normal vectors) and setting them to the same smooth-group. The pseudo code looks like this:


main {
  List polygonlist;
  int  current_smoothgroup_number;
  for (each polygon in polygonlist) {
    if (polygon's smoothgroup_number isn't set) {
      set polygon's smoothgroup_number to current_smoothgroup_number;
      call find_neighbours(polygon);
      current_smoothgroup_number++;
    }
  }
}

find_neighbours(polygon) {
  get all neighbour polygons;
  for (each neighbour_polygon) {
    if (   neighbour_polygon's smoothgroup_number isn't set
        && angle of normal vectors between polygon and neighbour_polygon < crease_angle) {
      set neighbour_polygon's smoothgroup_number to current_smoothgroup_number;
      call find_neighbours(neighbour_polygon); // Recursion! :-)
    }
  }     
}

Now imagine a sphere with several thousands polygons which all have soft edges…

One problem here is that the recursion depth can be as deep as the number of polygons (or worse if there is an bug).

An iterative form like this would be better


find_neighbours(polygon) {
   List work = new LinkedList();
   work.add(polygon);
   while (!work.isEmpty()) {
      Polygon p = (Polygon)work.remove(0);
      for (neighbours_of(p)) {
         if (   neighbour_polygon's smoothgroup_number isn't set 
           && angle of normal vectors between polygon and    neighbour_polygon < crease_angle) { 
      set neighbour_polygon's smoothgroup_number to current_smoothgroup_number; 
      work.add(neighbour_polygon);
          }      }
   }
}

Well, Mark, that’s smart. :slight_smile: Iterative has been the keyword. Haven’t been able to see the wood for the trees… although Blablah gave a good hint with the “checkpoint” idea.

Many thanks you all. There’s a lot we can learn here in the forum due to you good people.