Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a recursive function stop on random numbers? [duplicate]

Tags:

java

recursion

I wrote a small program shown below that counts how many times an infinite recursive loop will go before causing a StackOverflow error.

public class Testing {
    static void p(int i) {
        System.out.println("hello" + i);
        i++;
        p(i);
    }
    public static void main(String[] args) {
        p(1);
    }
}

The thing is, it errors on a different number each time, normally between 8000 and 9000. Can anyone explain why this happens?

EDIT: I'm using the Eclipse IDE, haven't tested it with other IDE's or the command line.

like image 818
AfterShock360 Avatar asked Mar 11 '19 18:03

AfterShock360


3 Answers

The JVM specs very nicely explain its behavior related to stack;

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

In the First Edition of The Java® Virtual Machine Specification, the Java Virtual Machine stack was known as the Java stack.

This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine stack may be chosen independently when that stack is created.

A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of Java Virtual Machine stacks, as well as, in the case of dynamically expanding or contracting Java Virtual Machine stacks, control over the maximum and minimum sizes.

The following exceptional conditions are associated with Java Virtual Machine stacks:

If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError.

If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

An important point from this excerpt as far as your question is concerned:

  • This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation.

Since you are not providing a stack size, JVM tries to dynamically expand the stack size as the function gets called recursively needing more stack memory. In each run, it may find different amount of dynamic memory for its stack depending on the availability of memory on your computer at that point of run. This is the reason you see a different value for the number of iterations it takes before throwing the SO error. If you configure (using Xss<size> JVM parameter) a smaller stack size to your program, you should see mostly identical number of recursions before the SO error.

like image 132
VHS Avatar answered Oct 23 '22 07:10

VHS


Might be related to how much real memory the computer can allocate to the program, while other programs and process are running in the computer

like image 1
riorio Avatar answered Oct 23 '22 05:10

riorio


StackOverflowError is a error. As a error, is related to the JVM (a error is not a Exception!).

This error occurs when your stack (or method execution stack) collides with your heap size (JVM's memory).

The size of JVM's heap can be defined, but from your stack no.

like image 1
FearX Avatar answered Oct 23 '22 06:10

FearX