This is explicitly JVM - and perhaps also architecture specific.
I measured the following:
static int i = 0; public static void rec0() { i++; rec0(); } public static void main(String[] args) { ... try { i = 0; rec0(); } catch (StackOverflowError e) { System.out.println(i); } ... }
using
Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
works on x86.
With a 20 MB Java stack ( -Xss20m ), the amortized cost ranged around 16-17 bytes per call. The lowest figure I saw was 16.15 bytes / frame. Therefore, I conclude that the cost is 16 bytes, and the rest is other (fixed) overhead.
A function that takes a single int has basically the same cost, 16 bytes / frame.
Interestingly, a function that accepts ten ints requires 32 bytes / frame. I'm not sure why the cost is so low.
The above results apply after compiling the JIT code. Before compilation, the cost of each frame is much higher. I still do not understand the way to reliably evaluate it. However, this means that you have no hope of reliably predicting the maximum depth of the recursion until you can reliably predict whether the recursive function was compiled by JIT.
All of this has been tested with the ulimit stack sizes of 128 KB and 8 MB. The results were the same in both cases.
NPE Dec 21 '12 at 19:24 2012-12-21 19:24
source share