Recursion Stack Overflow Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you java.lang.stackoverflowerror in java might have Meta Discuss the workings and policies of this java.lang.stackoverflowerror android site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or java.lang.stackoverflowerror in eclipse posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
What Is The Key Action To Avoid The Stack Overflow While Using Recursive Method
of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up java.lang.StackOverflowError due to recursion up vote 4 down vote favorite 2 My problem is that I usually get a java.lang.StackOverflowError when I use recursion. My question is - why does recursion cause stackoverflow so much java stack overflow increase stack size more than loops do, and is there any good way of using recursion to avoid stack overflow? This is an attempt to solve problem 107, it works well for their example but runs out of stack space for the problem it self. //-1 16 12 21 -1 -1 -1 16 -1 -1 17 20 -1 -1 12 -1 -1 28 -1 31 -1 21 17 28 -1 18 19 23 -1 20 -1 18 -1 -1 11 -1 -1 31 19 -1 -1 27 -1 -1 -1 23 11 27 -1 public class tries { public static int n=7,min=Integer.MAX_VALUE; public static boolean[][] wasHere=new boolean[n][60000]; public static void main(String[] args) { int[] lines=new int[n]; Arrays.fill(lines, -1000); lines[0]=0; int[][] networkMatrix=new int[n][n]; Scanner reader=new Scanner(System.in); int sum=0; for(int k=0; k here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn java.lang.stackoverflowerror in tomcat more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Stack overflows from deep recursion in Java? up vote 49 down http://stackoverflow.com/questions/18368406/java-lang-stackoverflowerror-due-to-recursion vote favorite 36 After some experience with functional languages, I'm starting to use recursion more in Java - But the language seems to have a relatively shallow call stack of about 1000. Is there a way to make the call stack bigger? Like can I make functions that are millions of calls deep, like in Erlang? I'm noticing this more and more when I do Project Euler problems. http://stackoverflow.com/questions/860550/stack-overflows-from-deep-recursion-in-java Thanks. java functional-programming stack overflow share|improve this question asked May 13 '09 at 21:35 Lucky 1,82152944 add a comment| 10 Answers 10 active oldest votes up vote 38 down vote accepted I guess you could use these parameters -ss Stacksize to increase the native stack size or -oss Stacksize to increase the Java stack size, The default native stack size is 128k, with a minimum value of 1000 bytes. The default java stack size is 400k, with a minimum value of 1000 bytes. http://edocs.bea.com/wls/docs61/faq/java.html#251197 EDIT: After reading the first comment (Chuck´s), as well as re reading the question and reading another answers, i´d like to clarify that i interpreted the question as just "increase stack size". I didn´t intend to say that you can have infinite stacks, such as in functional programming (a programming paradigm which i´ve only scratched its surface). share|improve this answer edited May 14 '09 at 1:12 answered May 13 '09 at 21:38 Tom 23k1495145 5 This can give you more levels, but the stack size is still limited. You won't be able to recurse infinitely like in a functional language with tall-call elimination. –Chuck May 13 '09 at 22:13 not really an answer... band aid solution & My Publications Saturday, July 4, 2009 Diagnosing and Resolving StackOverflowError A recent JavaWorld Community forum message (Stack Overflow after instantiating new object) reminded me that the basics of the StackOverflowError are not always understood http://marxsoftware.blogspot.com/2009/07/diagnosing-and-resolving.html well by people new to Java. Fortunately, the StackOverflowError is one of the easier of the runtime errors to debug and in this blog posting I will demonstrate how easy it often is to diagnose a StackOverflowError. Note that the potential for stack overflow is not limited to Java.Diagnosing the cause of a StackOverflowError can be fairly straightfoward if the code has been stack overflow compiled with the debug option turned on so that line numbers are available in the resulting stack trace. In such cases, it is typically simply a matter of finding the repeating pattern of line numbers in the stack trace. The pattern of repeating line numbers is helpful because a StackOverflowError is often caused by unterminated recursion. The repeating line numbers indicate the code that stack overflow error is being directly or indirectly recursively called. Note that there are situations other than unbounded recursion in which a stack overflow might occur, but this blog posting is limited to StackOverflowError caused by unbounded recursion.The relationship of recursion gone bad to StackOverflowError is noted in the Javadoc description for StackOverflowError that states that this Error is "Thrown when a stack overflow occurs because an application recurses too deeply." It is significant that StackOverflowError ends with the word Error and is an Error (extends java.lang.Error via java.lang.VirtualMachineError) rather than a checked or runtime Exception. The difference is significant. The Error and Exception are each a specialized Throwable, but their intended handling is quite different. The Java Tutorial points out that Errors are typically external to the Java application and thus normally cannot and should not be caught or handled by the application.I will demonstrate running into StackOverflowError via unbounded recursion with three different examples. The code used for these examples is contained in three classes, the first of which (and the main class) is shown next. I list all three classes in their entirety because line numbers are signifiHow To Resolve Stack Overflow Error In Java
How To Solve Stack Overflow Error In Java