Solved

Handling java.lang.StackOverflowError

Posted on 2003-10-25
4
695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 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