Why this code is throwing StackOverflowException and not OutOfMemoryException?

I have the following code and its throwing StackOverflowException because of nested constructor call, and I know memory gets allocated only when constructor runs successfully.

package interview;

public class StackOverflow {
    int i;
    StackOverflow()
    {
            System.out.println(" i= "+i);
        System.out.println("in Constructor");
        StackOverflow sOf = new StackOverflow();
        sOf.i=5;
        System.out.println(" i= "+i);
    }
    public static void main(String[] args) {
        System.out.println("in main");
        StackOverflow s = new StackOverflow();
        s.i=10;
        System.out.println(" i= "+s.i);
    }

}

So my doubt here is what is happening to the value of ‘i’ ? Is it getting stored somewhere in stack or heap?
In which case the above code can throw the following Exception?

OutOfMemoryException

4

OutOfMemoryError: Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

StackOverflowError: Thrown when a stack overflow occurs because an application recurses too deeply.

in which case it can throw OutOfMemoryError?

  • Your constructor is recursively calling itself going into more deep,
    stack overflowed before JVM was out of memory.

    instead of recursion use a for loop to create too much object. You
    might be able to see the OutOfMemoryError.

My question is what is happening to the value of i( it is getting stored heap or stack):

  • The JVM specification-2.5.2 clearly states that:

    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.

    As Heap is storage for objects and i is a field member of your
    StackOverflow class, it gets stored in the Heap

6

When you start your JVM, some memory is allocated to it from the operating system. The jvm uses this memory for its processing purpuses. JVM uses this memory in a number of ways – like stack and heap.

Whenever a method is called, the data like parameters, the return values and other local variables inside that method is kept on a stack. This stack is created when the method is called and is destroyed when the method completes its execution. If you keep on calling a method from a method , there would be some point at which the bottleneck will occur and it will show you a StackOverFlowError. But in case of heap – the JVM allocates memory to the new object from this heap. If you have references to objects and the free space left in heap is very less it will show you OutOfMemoryError.

When you start JVM you define how much RAM it can use use for processing. JVM divides this into certain memory locations for its processing purpose, two of those are Stack & Heap

OutOfMemoryError is related to Heap. If you have large objects (or) referenced objects in memeory, then you will see OutofMemoryError.

StackOverflowError is related to stack. All your local variables and methods calls related data will be on stack. For every method call one stack frame will be created and local as well as method call related data will be placed inside the stack frame. Once method execution is completed, stack frame will be removed. ONE WAY to reproduce this is, have infinite loop for method call, you will see stackoverflow error, because stack frame will be populated with method data for every call but it won’t be freed (removed).

In your case you are calling the constructer which causing the stack to overflow rather than heap, hence getting the StackOverflowError rather than OutOfMemoryError.

StackOverflow()
    {                        
        StackOverflow sOf = new StackOverflow(); //causing Stack to overflow.**                       
    }

The i‘s value will always be 0 i.e. the default value.

Please refer this.

Whenever you make a call to a function inside another function, the 2nd function comes at the top of the Stack. And since you have made recursive call to your constructor, it keeps on increasing your Stack size because of your new constructors. When there is no space left to create a new constructor entry in the main Stack, JVM throws StackOverflowException.

For OutofMemoryException, you just need to create as many Objects of, as much possible size such that there is no space left in your Heap.

Class variables and Objects are created in Heap and function calls, and local variables are maintained in Stack.

i is a class variable, so it is getting stored in Heap. And since it is not defined at the time of declaration it will get the default value of int i.e. 0. Your constructor never reaches the line where you are assigning the value 5 to i. So, it will always have the value 0.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật