Solved

Handling java.lang.StackOverflowError

Posted on 2003-10-25
4
689 Views
Last Modified: 2008-02-01
I created my own tree class to generate a game tree for a tictactoe game. The general idea is that the GameTree consisits of a root node which is an object i created called a TreeElement, and an array of GameTree called the children. Later on in the code, I have a recursive method called generateTree which, among other things, determines how many children a GameTree has (based on the current game configuration), and calls generateTree on each of the children. The terminating condition is when the GameTree has no children.

When I first tested my method on a small subtree of the my game tree, it worked fine. Then I tried a bigger subtree, and I got a java.lang.StackOverflowError. The frustrating thing is that after that, I got the same Error when i ran the method on any GameTree, even one with no children (in which case the method was just supposed to return).

The Java APIs says that a StackOverflowError occurs when a method recurses too deeply. So is there a way to increase the maximum depth of  a recursion  before it returns the Error?
Also, the fact that it still doesn't work for a subtree that it previously worked for seems to imply that there is a problem with memory allocation. System.gc() doesn't seem to work, so do you have any suggestions?
0
Comment
Question by:Rampage118
4 Comments
 
LVL 7

Expert Comment

by:tomboshell
ID: 9619126
I would say to make ensure that there is no addition of subnodes occuring in your recursion.  It may be that your ending condition is simply not being met.  Or you may be recursing on the same level, ie make sure that it IS changing levels.  Try debugging is sometime or adding a level counter that you can send to System.out.println(<something>);

It is not *neccessarily* that you are recursing too deeply when you get a StackOverflowError, you can get it with out-of-control situations like wild loops and such.  It is just that a terminal condition is not being met.  A tic-tac-toe game really should not have that large of a memory requirement (I would have suggested using the extended arguments to the virtual machine, but I don't think that is the problem.)  It sounds like a simple bug.
0
 
LVL 3

Expert Comment

by:savalou
ID: 9619324
You could set the maximum stack size used by java higher with java -Xss 64m and play with the value (64m, 128m, 256m, ...).
0
 
LVL 15

Accepted Solution

by:
jimmack earned 125 total points
ID: 9619356
Modifying the amount of memory available in this case will almost certainly not solve the problem.

The fact that this problem is occurring when you check your GameTree with no children means that it is very likely the situation that tomboshell has identified.  ie. your recursion end-condition is not being met.

Check your condition very carefully ;-)
0
 

Author Comment

by:Rampage118
ID: 9633978
Thanks for the tips guys. I eventually managed to find my bug. It's either I'm not smart or because I was coding at 03:00 am and I was tired. I prefer option B. Anywayz, what I did wrong was... In my constructor for the GameTree class,  I included a call to generateTree, the recursive method, and hance the infinite recursion.
Now I know. But thanks for your input. By the way, I got the game done, so if anyone wants the source code, email me: luqman@cam.wits.ac.za.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question