JVM Stack


Java Stack

Every Thread scheduled over the JVM is provided with a unique Java Stack specific to that Thread. Multiple Threads running under the same JVM don’t have access to each other’s Java Stack. Java Stack behaves as LIFO (Last In Fast Out).Threads only share the Heap Space with other Threads. The JVM performs only two operations over the Java Stack.


  1. Push Stack Frames.
  2. Pop Stack Frames


Stack Frames
Each Method of a Class gets its own Stack Frame. Each time a Method is executed by a Java Thread that Method’s Stack Frame is PUSHED in to the TOP of Java Stack of that particular Thread. Once the Thread completes the execution of the Method (or an Exception occurs and is not caught with in that Method’s body), its Stack Frame is POPED out of the Java Stack of that particular Thread and is discarded. 

Stack Frame holds the following:
  • Local Variables: The local variables section contains a method's parameters and local variables. Compilers place the parameters into the local variable array first, in the order in which they are declared. Instance Method's also have a hidden 'this' reference passed along with its parameters.
  • Operand Stack: It's like virtual registers, where values from the local variables are PUSHED into the operand stack, the instructions POPS those values from the operand stack works on them and then PUSHES back the result. The Result again gets POPED out of the operand stack and gets stored into the local variables.
  • Frame Data: includes data to support constant pool resolution, normal method return, and exception dispatch.

While running the below program, the following activities occurred over the Java Stack.


public class Check {  
      public void InnerMethod()  
      {  
           System.out.println(" Executing Inner Method ");  
      }  
      public void OuterMethod()  
      {  
           System.out.println(" Executing Outer Method ");  
           System.out.println(" Outer Method: About to call Inner Method ");  
           InnerMethod();  
           System.out.println(" Outer Method: Call to Inner Method returned ");  
      }  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           Check check = new Check();  
           check.OuterMethod();  
      }  
 }  

  1. As the program starts’ executing the Stack Frame of the ‘main’ Method is PUSHED into the Java Stack of the running thread.
  2. As the ‘OuterMethod’ is called from with the ‘main’ Method, the OuterMethod’s Stack Frame is PUSHED into the Java Stack of the running thread.
  3. As the ‘InnerMethod’ is called from with the ‘OuterMethod’ Method, the InnerMethod’s Stack Frame is PUSHED into the Java Stack of the running thread.
  4. As ‘InnerMethod’ completes execution its Stack Frame is POPPED out of the Stack Frame.
  5. As ‘OuterMethod’ completes execution its Stack Frame is POPPED out of the Stack Frame.
  6. As ‘main’ completes execution its Stack Frame is POPPED out of the Stack Frame.



No comments:

Post a Comment