I understand the importance of Single Responsibility Principle, but technically speaking do we have any upper bound on the number of local variables (that which are stored in stack frames) within each java method. And is that upper bound equal to the maximum stack size, ie., can i have a stack frame of size equal to the maximum stack size configured?
-
4how is question related to Single Responsibility Principle? – Ankur Oct 18 '12 at 18:44
-
1Ok. So the relation is that it is not good to have one single method with too many local variables. In which case it might be violating that principle. – Bajji Oct 18 '12 at 18:45
-
You can take a look at this post: - http://stackoverflow.com/questions/3700459/how-to-increase-to-java-stack-size – Rohit Jain Oct 18 '12 at 18:46
3 Answers
There is no upper bound in defining number of local variables. If you define too many variables which couldn't fit in a stack frame (or) JVM couldn't allocate a stack frame for that size, it will throw StackOverflowError
and exit.
There is good lecture by a stanford professor which may help you.
-
Does JVM create a fixed size stack frames? If so is there a way to increase it? I know that -Xss controls the stack size, similarly is there anything to control the frame size? – Bajji Oct 18 '12 at 19:01
-
1Stack frame size is dictated by your method definition. It is always the same for each method. – Marko Topolnik Oct 18 '12 at 19:19
-
1@Nambari, this is just not right to me. The size is known and it is maximum of 65,536. – Eugene Apr 12 '14 at 14:40
-
@Eugene: I may be wrong too. I would be happy to update answer with your comment, but before that it would be great if you can provide official reference to support your comment. – kosa Apr 13 '14 at 01:23
-
6@Nambari sure. The "i/aload" take as input one byte parameter, which servers as the index in the LVA (local variable array), which makes it up to 256 possible values, thus the max of 255 local variables. Unless **wide** is used (http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) look at the bottom; then it can use 16bit index in the LVA, thus 65536 local variables. – Eugene Apr 14 '14 at 13:02
-
@Eugene: I see what you mean. Though I agree with your information, I would say those inherent limitations are platform specific, not java language specific (java language specification document doesn't specify this information any where AFAIK). If another JVM implementer want to use higher number, I guess they are allowed to do. – kosa Apr 14 '14 at 13:54
-
@kosa this is not a limitation of any specific JVM implementation, it's a limitation in the specification of Java bytecode. You would need to change the bytecode instructions to support more locals, and once you're changing the bytecode specification I would argue that you're not really implementing a JVM anymore. – Earthcomputer May 19 '19 at 14:37
-
1
I'm surprised by the lack of straight answer to this clear question, so here goes: the JVM has a max frame size of 65535 locals and max stack size of 65535, with long
and double
consuming 2 slots per entry. From the JVM spec 4.11. Limitations of the Java Virtual Machine:
The greatest number of local variables in the local variables array of a frame created upon invocation of a method (§2.6) is limited to 65535 by the size of the max_locals item of the Code attribute (§4.7.3) giving the code of the method, and by the 16-bit local variable indexing of the Java Virtual Machine instruction set.
Note that values of type long and double are each considered to reserve two local variables and contribute two units toward the max_locals value, so use of local variables of those types further reduces this limit.
The size of an operand stack in a frame (§2.6) is limited to 65535 values by the max_stack field of the Code attribute (§4.7.3).
Note that values of type long and double are each considered to contribute two units toward the max_stack value, so use of values of these types on the operand stack further reduces this limit.
The Java language could in theory work around this JVM limitation by offloading locals and stack to the heap (since that can be waay larger), but in practice it doesn't really - javac
(at least as of Java 15) will just error out if you have 65535 locals or stack of depth 65535.

- 31,829
- 7
- 67
- 114
This will really be defined by your runtime and how much stack space is allocated, per process.

- 56,849
- 55
- 141
- 195