Results 1 to 11 of 11
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default 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();
    	}
    }
    Java Code:
    public class Test2 {
    		
    	void printMe(){
    		System.out.println("Method in Test2");
    		Test tester = new Test();
    		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.

    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]

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    That's infinite recursion, not an infinite loop. An example of an infinite loop would be
    Java Code:
    while(true) {
      // do anything or nothing
    }
    db

  6. #6
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    Why doesn't it just remove from the stack what is no longer needed to make space to continue the recursion?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    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.

  9. #9
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    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.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    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));
      }
    }
    This can be done a lot of different ways.

    You do understand the infinite recursion situation correctly though.

  11. #11
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Default

    another way to create an infinite loop is by this way:

    for (;; )
    {
    System.out.println("hello");
    }

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •