0

I have seen loops like for ( ; ; ) and while (true ).

Many programs use this technique to run loops infinitely. Is it possible to use recursion to apply the same technique?

nanofarad
  • 40,330
  • 4
  • 86
  • 117
Dale Steyn
  • 95
  • 1
  • 2
  • 10
  • 4
    There is no recursion here. You wouldn't want to use recursion instead of an infinite loop because of the stack overflow. – Sotirios Delimanolis Aug 14 '13 at 15:28
  • No, but [you can't accurately predict how deep it will go](http://stackoverflow.com/questions/13995885/how-to-predict-the-maximum-call-depth-of-a-recursive-method) – Bohemian Aug 14 '13 at 15:49
  • If JVM could optimize tail call, you would be able to make a recursion that continues infinitely. But [JVM does not allow for tail call optimization](http://stackoverflow.com/q/105834/335858), so you would need to stay with the loop. – Sergey Kalinichenko Aug 14 '13 at 15:50
  • Dale: Please accept an answer to this post so other visitors know which helped. – nanofarad Aug 14 '13 at 21:26

3 Answers3

4

No. Every level of recursion will place a new frame onto the stack for internal use and local variables.

With every level of depth added, you will eventually reach the end of a finite amount of memory dedicated for this stack. There is a shadow zone reserved for the last frame and any C++ area, so hitting that is enough for the stack to overflow.

Java methods generate code that checks that stack space is available a fixed distance towards the end of the stack so that the native code can be called without exceeding the stack space. This distance towards the end of the stack is called “Shadow Pages.” The size of the shadow pages is between 3 and 20 pages, depending on the platform. This distance is tunable, so that applications with native code needing more than the default distance can increase the shadow page size.

This is used in case you enter native code, which cannot detect end-of-stack reliably. If you do not have enough space on the stack for any recursion or calls made in native code, a true hard stack overflow could occur with nasty consequences.

Note that Java does not perform tail recursion optimization, so recursion is not turned into iteration by the compiler.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

Every time a function calls itself, this consumes stack space. Since stack is a finite resource, the program will eventually run out of stack space and be killed.

That said, it would be possible to create an infinite loop on a JVM that supported tail call optimization.

However, I am not aware of any such JVM. For a detailed discussion, see Does the JVM prevent tail call optimizations?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
-1

"If there is a chance to run a program infinitely using recursion in java?"

-No. But it may appear that some method or code snippets are trying to run infinitely but eventually they will error out. The main method below calling itself infinitely. You may think you are doing something infinitely but a certain amount of stack depending on the platform will eventually run out and jvm will throw the stack overflow error. So, technically no, you cannot.

public static void main(String... args){

    main(args);

}

Above is an example of bad recursion that will eventually lead to stack overflow error.

Edited per confusion.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • 1
    If it leads to stack overflow, it's not infinite, is it? – Sergey Kalinichenko Aug 14 '13 at 15:33
  • Could you please elaborate it??? – Dale Steyn Aug 14 '13 at 15:33
  • If it leads to stack overflow then the answer should be "No" – wonde Aug 14 '13 at 15:35
  • @dasblinkenlight - Yes, jvm will terminate it when no more stack left but main method thinks that it's calling itself infinitely. – Sajal Dutta Aug 14 '13 at 15:36
  • 1
    I would argue that it's _theoretically_ infinite, even if in practice it would end quickly. A `while (true) {}` loop would also end at some point — the computer might be shut down, or something in the CPU might burn out, or civilization might end in nuclear annihilation — though the practical duration would be much closer to infinity. – Reinstate Monica -- notmaynard Aug 14 '13 at 15:42
  • @iamnotmaynard Hello... " A while (true) {} loop would also end at some point". I'd read that most infinite loops are finite loops with special termination requirements. Are you stressing the same point here. Any idea on OS command processors programs? – Dale Steyn Aug 14 '13 at 15:46
  • infinite recursive calls can be made through tail recursion, should be supported by the language...lisp for example – boxed__l Aug 14 '13 at 15:50