Searching...
Sunday, 7 February 2016

What is JVM Stack? when will get StackOverflowError ?

23:23

What is JVM Stack? when will get StackOverflowError ?

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 . It holds local variables and partial results, and plays a part in method invocation and return.

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

package com.vinod.test;

public class TestStackOverFlow {

   public static void main(String[] args) {
      main(args);

   }

}

Output

Exception in thread "main" java.lang.StackOverflowError
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)
    at com.vinod.test.TestStackOverFlow.main(TestStackOverFlow.java:6)


That function calls itself repeatedly with no termination condition. Consequently, the stack fills up because each call has to push a return address on the stack, but the return addresses are never popped off the stack because the function never returns, it just keeps calling itself.

0 comments: