StackMapTable attribute

The types in each local variable and on the operand stack at every branch target and handler.

Appears on
Code
Since
class file 50.0 (Java 6)
javac writes it
for every method with a branch or an exception handler (class files 50.0 and later)
Needed by the JVM
yes: the JVM reads it to load, verify or run the class

Layout

TypeFieldWhat it holds
u2attribute_name_indexa Utf8: "StackMapTable"
u4attribute_lengthbytes after this field
u2number_of_entries
stack_map_frame[…]entriesone compressed frame per branch target, in pc order

What it is for

The types in each local variable and on the operand stack at every branch target and handler. With it the verifier checks a method in one pass, instead of inferring the types by iterating to a fixed point, which made class loading slower and verification complicated.

Frames are compressed: each one is stored as a delta from the previous frame (same, same_locals_1_stack_item, chop, append, full_frame), and the pc as an offset from the previous frame's pc.

It became mandatory with class file version 51 (Java 7). It is also why bytecode generators have to compute frames, which ASM's COMPUTE_FRAMES does.

How common it is

JDK 25.0.5's java.base has 23,998 StackMapTable attributes, in 4,132 of its 7,391 classes (23,998 on Code attributes).

A real one, byte by byte

From Hello.class (compiled by javac 25), on method main, in its Code attribute:

OffsetBytesFieldValue
102413 bytesStackMapTable
102400 26attribute_name_index#38
102600 00 00 07attribute_length7
103000 02number_of_entries2
10321 bytesframe 0@11 same
10320bframe_type11
10334 bytesframe 1@13 same_locals_1_stack_item
103341frame_type65
10343 bytesstack
103407tagObject
103500 27cpool_index#39

Open in the viewer

Specification: JVMS §4.7, The StackMapTable Attribute (Java SE 25).