Java: recursive constructor invocation and stackoverflow error

Please help to understand why the following code

public class HeapQn1 {

    /**
     * @param args
     */
    public HeapQn1() {
        new HeapQn1();
    }

    static HeapQn1 n = new HeapQn1();

    public static void main(String[] args) {

    }

}

      

leads to

java.lang.StackOverflowError
    at com.rg.test.interview.HeapQn1.<init>(HeapQn1.java:8)
    at com.rg.test.interview.HeapQn1.<init>(HeapQn1.java:9)
    ...

      

As per my understanding, the memory allocation for the object happens in heap memory and I was expecting an OutOfMemoryError as at some point the heap memory will be full due to the creation of duplicate objects.

While researching, I came across the java constructor is considered a method and explains the StackOverflowError until I read the following thread.

When is a constructor called in java?

which says

3. The object is fully constructed/created when the constructor returns.

      

From what I could gather, a constructor is a method, and since heap memory is much larger than stack memory, calling the recursive constructor resulted in a StackOverflowError. Is it correct?

Since no object in the give code will be fully instantiated, will the stack frame be allocated for the constructor?

- edit-- For the marked duplicates, I understand what the StackoverflowError is. I mentioned in the question "About research, I stumbled upon the fact that the java constructor is considered a method and explains the StackOverflowError". My question is to understand if the constructor gets a stack frame like other methods, since the object creation is not complete until the constructor returns. Hope this clarifies.

+3


source to share


5 answers


Whenever a constructor is called, it return address

is pushed onto the stack . Since the stack is finite and less heap memory, you get an error, for example StackOverflowError

, not OutOfMemoryError

.

A constructor is a method, and since the heap memory is much larger than the stack memory, calling the recursive constructor resulted in a StackOverflowError. Is it correct?



Yes, your wild guess is absolutely correct. Hooray!

+2


source


A constructor is a method, aka a function. Each time you call this, a block of memory is allocated on the stack to store the function variables.

Your code automatically creates calls to the constructor function, allocating memory on the stack until the memory runs out.

You get StackOverflowError

, not OutOfMemoryError

because the amount of memory allocated for the stack is less than the amount of memory allocated for the heap.



EDITOR: I ran a few tests using your code. I specified 8M ( -Xms8M -Xmx8M

) heap memory space and 100M ( -Xss100M

) stack memory space . The calculation always results in an error StackOverflowError

.

Then it may mean that memory will not be allocated on the heap in this case . As you stated in your question:

The object is fully constructed / created when the constructor returns.

+1


source


You're right: the stack is much smaller than the heap and the object won't be completely created.

0


source


Basically what you said is correct, stack space ends up before heap space does.

0


source


According to Java, all reference variables are stored in the stack memory space, and the stack memory space is less than the heap space, which is what we get a stackOverFlowException.

0


source







All Articles