Solved

Handling java.lang.StackOverflowError

Posted on 2003-10-25
4
681 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now