What is a Stack Frame?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A stack frame is the JVM's working area for one method call. Every time a thread calls a method, the JVM creates a new frame on that thread's stack. A recursive call gets its own frame and its own local values.
A frame mainly contains local-variable slots, an operand stack, and information for linking and returning. Method arguments and local values go into the local slots. Bytecode uses the operand stack to perform calculations.
For example, a call to sum(20, 22) places 20 and 22 in local slots. The bytecode loads them onto the operand stack, adds them, and returns 42 to the caller. If the method throws an unhandled exception, the frame is removed while the JVM looks for a handler in an earlier call.
The frame exists only for that invocation. When the method completes, its frame is destroyed. Too many unfinished calls, such as recursion without a working base case, can fill the thread's stack and cause StackOverflowError.
What is a Stack Frame
Direct Answer
A JVM frame is the per-invocation data structure on one thread's JVM stack. It contains local-variable slots, an operand stack, and information needed for dynamic linking, normal return, and exception dispatch. One frame is created for every method invocation and destroyed when that invocation completes.
Deep Dive & Architecture
Values inside one method invocation
Many JVM bytecodes are stack-oriented. For an integer addition, instructions load values from indexed local slots onto the operand stack, iadd removes the top two integers and pushes their sum, and another instruction stores or returns that result. The operand stack is temporary working state inside this invocation; it is not the thread's whole JVM stack.
Object fields remain part of their objects. A local or operand slot can hold an object reference, and field instructions use that reference to read or write the target object. This distinction prevents the common mistake of drawing the complete object inside the frame.
Connect the frame to its class
A frame refers to the runtime constant pool for the class of the current method. Symbolic method and field references can be resolved through that structure. The frame does not contain a full private copy of every class constant; it carries the execution link needed by this call.
Normal and exceptional method completion
A normal return transfers a result if the method has one and restores the caller as current. An unhandled exception causes abrupt completion and frame unwinding while the JVM searches callers for a handler. In both cases, this invocation's frame ends.
The class file records maximum local and operand requirements, but a JVM chooses its concrete representation. Methods can therefore have different frame costs, and portable Java code should not assume an exact byte count per call.
Code Example
class StackFrame {
static int sum(int left, int right) {
int result = left + right;
return result;
}
public static void main(String[] args) {
System.out.println(sum(20, 22));
}
}
Compile and run javap -c StackFrame to connect local loads, operand-stack addition, and return.
Check your understanding
Answer these without looking back at the article.
- What are the main parts of a JVM frame?
- Why does recursion create new local variables for each call?
- What happens to a frame when its method throws an uncaught exception?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.