3

Running the following code with the -Xss160k option results in an increased depth reached each time I call it until I hit a threshold.

int reached = 0;

private void fillStackToAlmostDead() {
    for (int i = 0; i < 10; i++)
        try {
            reached = 0;
            exhausting(-1, null);
        } catch (StackOverflowError e) {
            System.out.println(String.format("Filled with %d calls", reached));
        }
}

private void exhausting(int depth, Callable after) {
    if (depth < 0)
        reached += 1;

    if (depth != 0)
        exhausting(depth - 1, after);
}

Output:

Filled with 650 calls
Filled with 1408 calls
Filled with 1408 calls
Filled with 1408 calls
Filled with 1408 calls
Filled with 1408 calls
Filled with 2816 calls
Filled with 2816 calls
Filled with 2816 calls
Filled with 2816 calls

I am a bit confused at this. Is there some optimization going on within the JVM so that successive calls cost less stack space?

Derongan
  • 790
  • 6
  • 24
  • Im guessing its due to the JIT compiler. Still not sure exactly what is doing though. I can remove the branching and still see the same result. – Derongan Apr 25 '18 at 00:42

0 Answers0