Java Virtual Machine (JVM) Stack Area
The Java Virtual Machine is responsible for running Java applications, and it manages various memory areas, one of which is the Stack Area. In this article, we are going to discuss about JVM Stack Area in depth.
- In Java, each thread has its stack called the Run-Time Stack, created when the thread starts.
- The memory for a Java Virtual Machine stack does not need to be contiguous.
- The JVM manages this stack by pushing and popping frames. Each method call creates a new stack frame that stores:
- Method call information: Stores details about the method being executed.
- Local variables: Stores local variables defined within the method.
- Method parameters: Stores parameters passed to the method.
- Return address: Tracks where to return once the method completes.
After all method calls are completed, the stack is emptied and destroyed by the JVM. The stack data is thread-specific, ensuring thread safety for local variables. Each entry in the stack is called a Stack Frame or Activation Record.
Working of Stack
When a method is called a new stack frame is created and pushes onto the stack, this stack frame contains all the information needed for the method execution. The JVM executes the method’s code, using the local variables and parameters stored in the stack frame. Once the method execution is complete, the stack frame is popped off the stack, and control returns to the calling method.
Example: Here, the below code demonstrate how actually the stack works.
// Java Program to Demonstrate the working of stack
import java.io.*;
class Geeks {
public static int add(int a, int b) {
return a + b;
}
public static void main (String[] args) {
int ans = add(5, 5);
System.out.println("Answer: " + ans);
}
}
Output
Answer: 10
Explanation:
- When main method is called, the stack frame for the main is created.
- Inside the main, the add method is called with the parameters, now the new stack frame for the add is created.
- The
add
method executes using the provided parameters - After the
add
method returns, its stack frame is popped off and control returns tomain
.
Below diagram demonstrates the stack area of JVM:

Stack Frame Structure
The stack frame basically consists of three parts. The three parts are
- Local Variable Array
- Operand Stack
- Frame Data
When JVM invokes a Java method, first it checks the class data to determine the number of words (size of the local variable array and operand stack, which is measured in words for each individual method) required by the method in the local variables array and operand stack. It creates a stack frame of the proper size for invoked method and pushes it onto the Java stack.
1. Local Variable Array (LVA)
- The local variables part of the stack frame is organized as a zero-based array of words.
- It contains all parameters and local variables of the method.
- Each slot or entry in the array is of 4 Bytes.
- Values of type int, float, and reference occupy 1 entry or slot in the array i.e. 4 bytes.
- Values of double and long occupy 2 consecutive entries in the array i.e. 8 bytes total.
- Byte, short, and char values will be converted to int type before storing and occupy 1 slot i.e. 4 Bytes.
- But the way of storing Boolean values is varied from JVM to JVM. But most of the JVM gives 1 slot for Boolean values in the local variable array.
- The parameters are placed into the local variable array first, in the order in which they are declared.
Example: Let us consider a class Example having a method bike() then the local variable array will be as shown in the below diagram:
// Class Declaration
class Example
{
public void bike(int i, long l, float f, double d, Object o, byte b)
{
}
}

2. Operand Stack (OS)
- The JVM uses an operand stack as a workspace for temporary calculations.
- Unlike the local variable array, which you access using an index, the operand stack works with special instructions. These instructions can push values onto the stack, pop values off, and perform operations using those values.
Example: Here is how a JVM will use this below code that would subtract two local variables that contain two ints and store the int result in a third local variable:

- So here first two instructions iload_0 and iload_1 will push the values in the operand stack from a local variable array. And instruction isub will subtract these two values and store the result back to the operand stack and after istore_2 the result will pop out from the operand stack and will store into a local variable array at position 2.

3. Frame Data (FD)
- It contains all symbolic references (constant pool resolution) and normal method returns related to that particular method.
- It also contains a reference to the Exception table which provides the corresponding catch block information in the case of exceptions.