Have been reading about the JVM internals recently and had a question about one of its elements - the method's operand stack. Does it have a max size and can we overflow it? Can we do it manually or what condition may cause it?
-
The maximum size is represented by the memory allocated to the JVM minus whatever memory is already in use. – Jacob G. Apr 22 '17 at 20:00
1 Answers
The maximum size is set per method, with the MaxStack attribute. Creating a bigger operand stack (or an operand stack of indeterminate size) causes a verification error, so in a sense you can't really dynamically overflow it, it's either correct (staying within the limit) or it doesn't even load.
Since the type of the MaxStack attribute is uint16, you can specify an operand stack size up to
216-1 slots. Wide types take 2 slots, same as for local variables. So a really big expression in Java can theoretically be uncompilable, but in practice javac
runs out of stack space (actual stack space, not operand stack) a long time before that.
Exceeding the limit set by the MaxStack attribute is easy when the attribute has been set incorrectly, which occasionally happens when ClassWriter is used without enough care.

- 61,398
- 6
- 86
- 164
-
does this mean if we create a method too big than it ll cause verfication error during compile time? – amarnath harish May 24 '18 at 16:36
-
@amarnathharish it means that if a method that needs too many stack entries is created, it cannot even be compiled to a class file – harold May 24 '18 at 17:12
-
so big local variable array and operand stack is not possible. i get it. buy why stack space is deprived first than stack frame? – amarnath harish May 26 '18 at 03:53