Results 1 to 11 of 11
Thread: infinite loop problem
- 05-11-2011, 03:52 AM #1
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
infinite loop problem
I know there is a better way to create an infinite loop than this but I was just playing around and created what I thought would be an infinite loop.
Here is the code:
Java Code:public class Test { void printMe(){ System.out.println("Method in Test"); Test2 tester = new Test2(); tester.printMe(); } public static void main(String[] args){ Test2 tester = new Test2(); tester.printMe(); } }Problem is, it works for a few seconds and then stops and I can't see the reason why. I am just messing around really and not trying to create anything, but it's been a good learning experience.Java Code:public class Test2 { void printMe(){ System.out.println("Method in Test2"); Test tester = new Test(); tester.printMe(); } }
Can any one tell me why my loop fails
Here is the output:
Java Code:Method in Test Method in Test2 Method in Test Method in Test2 Method in Test Method in Test2 Method in Test Method in Test2 Method in Test [COLOR="Red"]Exception in thread "main" java.lang.StackOverflowError at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source) at java.nio.charset.CharsetEncoder.encode(Unknown Source) at sun.nio.cs.StreamEncoder.implWrite(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.PrintStream.write(Unknown Source) at java.io.PrintStream.print(Unknown Source) at java.io.PrintStream.println(Unknown Source) at Test2.printMe(Test2.java:5) at Test.printMe(Test.java:6) at Test2.printMe(Test2.java:7) at Test.printMe(Test.java:6) at Test2.printMe(Test2.java:7) at Test.printMe(Test.java:6) at Test2.printMe(Test2.java:7)[/COLOR]
-
It's because you have succeeded!!! you have created your "infinite loop, that loop forever -- or until the stack memory runs out, and then it will crumple up into a little ball and quietly die.
- 05-11-2011, 04:00 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It overflows the stack, only so much can be out on it before the next add will overflow it. Each time a method is called its pushed onto the stack. Since the two methods create an object and call back and forth the methods never finish and get pushed off the stack. Because of this it eventually builds up and overflows the stack.
Last edited by sunde887; 05-11-2011 at 04:02 AM.
- 05-11-2011, 04:02 AM #4
To add to the above reply, everytime a method is called it is added to what is known as the method call stack. This stack has a finite amount of memory. If you add too many calls to the stack it will throw the exception that you see and the program will die a painful death.
- 05-11-2011, 04:04 AM #5
That's infinite recursion, not an infinite loop. An example of an infinite loop would be
dbJava Code:while(true) { // do anything or nothing }
- 05-11-2011, 04:30 AM #6
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
Why doesn't it just remove from the stack what is no longer needed to make space to continue the recursion?
- 05-11-2011, 04:33 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Because none of the methods ever get finished, it can't lose any of the methods because something important could be lost. Either way, the methods will never end, all they do is call eachother infinitely. There is no end.
- 05-11-2011, 04:35 AM #8
A method call is only removed from the stack when the method finishes executing and exits. Since your code never finishes executing any of the methods they cannot be removed. You simply keep on piling more method calls on top of each other. Think about a stack of plates. You can only access the top plate. You cannot get to any of the other plates until the top one is removed. But you keep on adding plates to the stack and never get to remove one.
- 05-11-2011, 04:40 AM #9
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
So if a method calls another method, then the method that did the calling is held in memory. However in this example, the method that did the calling never gets completed because the second method calls a new instance of the first method and also doesn't complete. Then this continues until lots of these methods are held in memory until the memory is full.
Am I right? If I am, I think that I am on my way to begin understanding recursion which is of course the key to understanding recursion.
- 05-11-2011, 04:47 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That's pretty correct. This methods recursion isn't quite like normal recursive calls but it is. A good way to understand recursion is to try and write some. For example, this is a tail recursive factorial method
This can be done a lot of different ways.Java Code:public int factorial(int n){ fact-iter(1, n); } public int fact-iter(int total, int n){ if(n == 1){ return total; } else{ fact-iter((total * n), (n - 1)); } }
You do understand the infinite recursion situation correctly though.
- 05-11-2011, 09:24 PM #11
Similar Threads
-
infinite loop
By javapink in forum New To JavaReplies: 19Last Post: 03-06-2011, 02:28 AM -
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08-13-2010, 11:45 PM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM -
Infinite Loop
By rclausing in forum New To JavaReplies: 2Last Post: 01-23-2010, 10:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks