Why does this method print 4?

JavaJvmStack Overflow

Java Problem Overview


I was wondering what happens when you try to catch an StackOverflowError and came up with the following method:

class RandomNumberGenerator {

    static int cnt = 0;

    public static void main(String[] args) {
        try {
            main(args);
        } catch (StackOverflowError ignore) {
            System.out.println(cnt++);
        }
    }
}

Now my question:

Why does this method print '4'?

I thought maybe it was because System.out.println() needs 3 segments on the call stack, but I don't know where the number 3 comes from. When you look at the source code (and bytecode) of System.out.println(), it normally would lead to far more method invocations than 3 (so 3 segments on the call stack would not be sufficient). If it's because of optimizations the Hotspot VM applies (method inlining), I wonder if the result would be different on another VM.

Edit:

As the output seems to be highly JVM specific, I get the result 4 using
Java(TM) SE Runtime Environment (build 1.6.0_41-b02)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)


Explanation why I think this question is different from https://stackoverflow.com/questions/15083318/understanding-java-stack:

My question is not about why there is a cnt > 0 (obviously because System.out.println() requires stack size and throws another StackOverflowError before something gets printed), but why it has the particular value of 4, respectively 0,3,8,55 or something else on other systems.

Java Solutions


Solution 1 - Java

I think the others have done a good job at explaining why cnt > 0, but there's not enough details regarding why cnt = 4, and why cnt varies so widely among different settings. I will attempt to fill that void here.

Let

  • X be the total stack size
  • M be the stack space used when we enter main the first time
  • R be the stack space increase each time we enter into main
  • P be the stack space necessary to run System.out.println

When we first get into main, the space left over is X-M. Each recursive call takes up R more memory. So for 1 recursive call (1 more than original), the memory use is M + R. Suppose that StackOverflowError is thrown after C successful recursive calls, that is, M + C * R <= X and M + C * (R + 1) > X. At the time of the first StackOverflowError, there's X - M - C * R memory left.

To be able to run System.out.prinln, we need P amount of space left on the stack. If it so happens that X - M - C * R >= P, then 0 will be printed. If P requires more space, then we remove frames from the stack, gaining R memory at the cost of cnt++.

When println is finally able to run, X - M - (C - cnt) * R >= P. So if P is large for a particular system, then cnt will be large.

Let's look at this with some examples.

Example 1: Suppose

  • X = 100
  • M = 1
  • R = 2
  • P = 1

Then C = floor((X-M)/R) = 49, and cnt = ceiling((P - (X - M - C*R))/R) = 0.

Example 2: Suppose that

  • X = 100
  • M = 1
  • R = 5
  • P = 12

Then C = 19, and cnt = 2.

Example 3: Suppose that

  • X = 101
  • M = 1
  • R = 5
  • P = 12

Then C = 20, and cnt = 3.

Example 4: Suppose that

  • X = 101
  • M = 2
  • R = 5
  • P = 12

Then C = 19, and cnt = 2.

Thus, we see that both the system (M, R, and P) and the stack size (X) affects cnt.

As a side note, it does not matter how much space catch requires to start. As long as there is not enough space for catch, then cnt will not increase, so there are no external effects.

EDIT

I take back what I said about catch. It does play a role. Suppose it requires T amount of space to start. cnt starts to increment when the leftover space is greater than T, and println runs when the leftover space is greater than T + P. This adds an extra step to the calculations and further muddies up the already muddy analysis.

EDIT

I finally found time to run some experiments to back up my theory. Unfortunately, the theory doesn't seem to match up with the experiments. What actually happens is very different.

Experiment setup: Ubuntu 12.04 server with default java and default-jdk. Xss starting at 70,000 at 1 byte increments to 460,000.

The results are available at: https://www.google.com/fusiontables/DataSource?docid=1xkJhd4s8biLghe6gZbcfUs3vT5MpS_OnscjWDbM I've created another version where every repeated data point is removed. In other words, only points that are different from the previous are shown. This makes it easier to see anomalies. https://www.google.com/fusiontables/DataSource?docid=1XG_SRzrrNasepwZoNHqEAKuZlHiAm9vbEdwfsUA

Solution 2 - Java

This is the victim of bad recursive call. As you are wondering why the value of cnt varies, it is because the stack size depends on the platform. Java SE 6 on Windows has a default stack size of 320k in the 32-bit VM and 1024k in the 64-bit VM. You can read more here.

You can run using different stack sizes and you will see different values of cnt before the stack overflows-

> java -Xss1024k RandomNumberGenerator

You don't see the value of cnt being printed multiple times even though the value is greater than 1 sometimes because your print statement is also throwing error which you can debug to be sure through Eclipse or other IDEs.

You can change the code to the following to debug per statement execution if you'd prefer-

static int cnt = 0;
    
public static void main(String[] args) {                  
            
    try {     

        main(args);   
                  
    } catch (Throwable ignore) {

        cnt++;

        try { 

            System.out.println(cnt);

        } catch (Throwable t) {   

        }        
    }        
}

UPDATE:

As this getting a lot more attention, let's have another example to make things clearer-

static int cnt = 0;

public static void overflow(){
  
    try {	  

   	  overflow();	  
    
	} catch (Throwable t) {
	  
	  cnt++;		 	      	  
    
	}
  
}
  
public static void main(String[] args) {
	
	overflow();
	System.out.println(cnt);
		
}

We created another method named overflow to do a bad recursion and removed the println statement from the catch block so it doesn't start throwing another set of errors while trying to print. This works as expected. You can try putting System.out.println(cnt); statement after cnt++ above and compile. Then run multiple times. Depending on your platform, you may get different values of cnt.

This is why generally we do not catch errors because mystery in code is not fantasy.

Solution 3 - Java

The behavior is dependent upon the stack size (which can be manually set using Xss. The stack size is architecture specific. From JDK 7 source code:

> // Default stack size on Windows is determined by the executable (java.exe
> // has a default value of 320K/1MB [32bit/64bit]). Depending on Windows version, changing
> // ThreadStackSize to non-zero may have significant impact on memory usage.
> // See comments in os_windows.cpp.

So when the StackOverflowError is thrown, the error is caught in catch block. Here println() is another stack call which throws exception again. This gets repeated.

How many times it repeates? - Well it depends on when JVM thinks it is no longer stackoverflow. And that depends on the stack size of each function call (difficult to find) and the Xss. As mentioned above default total size and size of each function call (depends on memory page size etc) is platform specific. Hence different behavior.

Calling the java call with -Xss 4M gives me 41. Hence the correlataion.

Solution 4 - Java

I think the number displayed is the number of time the System.out.println call throws the Stackoverflow exception.

It probably depend on the implementation of the println and the number of stacking call it is made in it.

As an illustration:

The main() call trigger the Stackoverflow exception at call i. The i-1 call of main catch the exception and call println which trigger a second Stackoverflow. cnt get increment to 1. The i-2 call of main catch now the exception and call println. In println a method is called triggering a 3rd exception. cnt get increment to 2. this continue until println can make all its needed call and finally display the value of cnt.

This is then dependent of the actual implementation of println.

For the JDK7 either it detect cycling call and throws the exception earlier either it keep some stack resource and throw the exception before reaching the limit to give some room for remediation logic either the println implementation doesn't make calls either the ++ operation is done after the println call thus is by pass by the exception.

Solution 5 - Java

  1. main recurses on itself until it overflows the stack at recursion depth R.
  2. The catch block at recursion depth R-1 is run.
  3. The catch block at recursion depth R-1 evaluates cnt++.
  4. The catch block at depth R-1 calls println, placing cnt's old value on the stack. println will internally call other methods and uses local variables and things. All these processes require stack space.
  5. Because the stack was already grazing the limit, and calling/executing println requires stack space, a new stack overflow is triggered at depth R-1 instead of depth R.
  6. Steps 2-5 happen again, but at recursion depth R-2.
  7. Steps 2-5 happen again, but at recursion depth R-3.
  8. Steps 2-5 happen again, but at recursion depth R-4.
  9. Steps 2-4 happen again, but at recursion depth R-5.
  10. It so happens that there is enough stack space now for println to complete (note that this is an implementation detail, it may vary).
  11. cnt was post-incremented at depths R-1, R-2, R-3, R-4, and finally at R-5. The fifth post-increment returned four, which is what was printed.
  12. With main completed successfully at depth R-5, the whole stack unwinds without more catch blocks being run and the program completes.

Solution 6 - Java

After digging around for a while, I can't say that I find the answer, but I think it's quite close now.

First, we need to know when a StackOverflowError will be thrown. In fact, the stack for a java thread stores frames, which containing all the data needed for invoking a method and resume. According to Java Language Specifications for JAVA 6, when invoking a method,

> If there is not sufficient memory available to create such an activation frame, an StackOverflowError is thrown.

Second, we should make it clear what is "there is not sufficient memory available to create such an activation frame". According to Java Virtual Machine Specifications for JAVA 6,

> frames may be heap allocated.

So, when a frame is created, there should be enough heap space to create a stack frame and enough stack space to store the new reference which point to the new stack frame if the frame is heap allocated.

Now let's go back to the question. From the above, we can know that when a method is execute, it may just costs the same amount of stack space. And invoking System.out.println (may) needs 5 level of method invocation, so 5 frames need to be created. Then when StackOverflowError is thrown out, it has to go back 5 times to get enough stack space to store 5 frames' references. Hence 4 is print out. Why not 5? Because you use cnt++. Change it to ++cnt, and then you will get 5.

And you will notice that when the size of stack go to a high level, you will get 50 sometimes. That is because the amount of available heap space need to be taken into consideration then. When the stack's size is too large, maybe heap space will run out before stack. And (maybe) the actual size of stack frames of System.out.println is about 51 times of main, therefore it goes back 51 times and print 50.

Solution 7 - Java

This is not exactly an answer to the question but I just wanted to add something to the original question that I came across and how I understood the problem:

In the original problem the exception is caught where it was possible:

For example with jdk 1.7 it is caught at first place of occurence.

but in earlier versions of jdk it looks like the exception is not being caught at the first place of occurence hence 4, 50 etc..

Now if you remove the try catch block as following

public static void main( String[] args ){
    System.out.println(cnt++);
    main(args);
}

Then you will see all the values of cnt ant the thrown exceptions (on jdk 1.7).

I used netbeans to see the output, as the cmd will not show all the output and exception thrown.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflrnbView Question on Stackoverflow
Solution 1 - JavaJohn TsengView Answer on Stackoverflow
Solution 2 - JavaSajal DuttaView Answer on Stackoverflow
Solution 3 - JavaJatinView Answer on Stackoverflow
Solution 4 - JavaKazaagView Answer on Stackoverflow
Solution 5 - JavaCraig GidneyView Answer on Stackoverflow
Solution 6 - JavaJayView Answer on Stackoverflow
Solution 7 - Javame_digvijayView Answer on Stackoverflow