跪拜 Guibai
← All articles
Backend · Java · AI Programming

The Anatomy of a JVM Frame: `locals=` vs `stack=` in `javap` Output

By 金銀銅鐵 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Reading `javap` output becomes straightforward once you see that `locals=` counts slots (including `this`) while `args_size=` counts parameters, a distinction that matters for debugging and for any tooling that inspects class files. The compile-time determination of both sizes is also why the JVM can allocate frames with a single linear memory reserve, keeping method invocation cheap and predictable.

Summary

Every method call in the JVM allocates a frame holding three things: a local variable array, an operand stack, and a reference to the run-time constant pool. The local variable array starts at index 0; instance methods reserve slot 0 for `this` and place arguments from slot 1 onward. `long` and `double` values consume two slots, while `int`, `float`, `reference`, and `returnAddress` take one.

The operand stack is a LIFO stack with a maximum depth fixed at compile time. That maximum depth, along with the local variable slot count, gets stored in the `Code` attribute and printed by `javap` as `stack=` and `locals=`. The article traces a simple `add(int, int)` method through its four bytecode instructions, showing exactly how the stack depth peaks at 2 after `iload_2` and how `locals=3` corresponds to `this`, `a`, and `b`.

A separate value, `args_size=`, counts the number of arguments including `this` for instance methods. `locals` and `args_size` diverge when a parameter is `long` or `double`, because each such parameter occupies two slots but still counts as one argument.

Takeaways
— Each method invocation creates a new frame containing a local variable array, an operand stack, and a run-time constant pool reference.
— Instance methods put `this` in local variable slot 0; parameters then occupy consecutive slots starting at slot 1, while static methods start parameters at slot 0.
— `long` and `double` occupy two local variable slots and contribute two units of operand stack depth; all other primitive types and references contribute one.
— The maximum operand stack depth and the local variable array size are compile-time constants stored in the `Code` attribute and surfaced by `javap -v` as `stack=` and `locals=`.
— `args_size` counts arguments including `this` for instance methods, whereas `locals` counts the total number of slots, so methods with `long` or `double` parameters have `locals` greater than `args_size`.
— `LocalVariableTable` with variable names is only generated when `javac -g` is used, but `locals=` and `stack=` appear regardless.
Conclusions

The byte offsets printed by `javap` are positional addresses, not instruction indexes; they only coincide with instruction sequence numbers when every instruction is exactly one byte long, which breaks as soon as multi-byte opcodes or operands appear.

Because the local variable array size and operand stack depth are fixed at compile time, the JVM never needs to compute frame capacities at runtime, which is a key reason method calls can be so cheap in terms of allocation.

Concepts & terms
Frame (JVM)
A per-call data structure in the JVM that holds the local variable array, the operand stack, and a reference to the run-time constant pool. A new frame is created on every method invocation and destroyed when the method returns.
Local variable slot
The storage unit in a frame's local variable array. `int`, `float`, `reference`, and `returnAddress` occupy one slot; `long` and `double` occupy two slots, accessible only via the smaller index.
Operand stack
A LIFO stack inside each frame where bytecode instructions pop operands and push results. Its maximum depth is determined at compile time and recorded in the `Code` attribute.
args_size vs locals
`args_size` counts the number of parameters, including `this` for instance methods. `locals` counts the total number of slots in the local variable array. They differ when parameters are `long` or `double`, which take two slots each.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗