问题
We have an out of memory error for a heap but ( just asking out of curiosity ) is there an equivalent limit on size of an individual stack ? If not then what prevents such an overflow if excess memory is needed by a stack frame ( like thousands of local variable etc ) ?
回答1:
If a thread requests more stack space than it has available it receives a StackOverflowError.
http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html
The size of an individual stack frame is determined at compile time and stored in the class file together with the method's code. Actually there are two fields: the size of the local variable array, and the depth of the operand stack. Both are limited to 2^16-1. http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1546
回答2:
The space allocated to the runtime stack is for all of your stack frames on a single execution thread combined, i think.
The default size is different for most OSes. Have a look at these docs. You can increase the size if need be with the -Xss
JVM parameter.
http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html
回答3:
Each thread gets its own allocation of stack space. You can set how big this allocation is with the -Xss option on the Java command line; the default is different on different platforms, but it's always a "per thread" amount.
The limit is an amount of memory per thread, not a number of entries on the stack.
来源:https://stackoverflow.com/questions/19582194/is-there-a-upper-limit-on-stack-frame-size