Handling java.lang.StackOverflowError

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?
Rampage118Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tomboshellCommented:
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
savalouCommented:
You could set the maximum stack size used by java higher with java -Xss 64m and play with the value (64m, 128m, 256m, ...).
0
jimmackCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rampage118Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.