The Anatomy of a JVM Frame: `locals=` vs `stack=` in `javap` Output
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.
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.
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.