What causes a java.lang.StackOverflowError

Check for any recusive calls for methods. Mainly it is caused when there is recursive call for a method. A simple example is public static void main(String… args) { Main main = new Main(); main.testMethod(1); } public void testMethod(int i) { testMethod(i); System.out.println(i); } Here the System.out.println(i); will be repeatedly pushed to stack when the … Read more

Java stack overflow error – how to increase the stack size in Eclipse?

Open the Run Configuration for your application (Run/Run Configurations…, then look for the applications entry in ‘Java application’). The arguments tab has a text box Vm arguments, enter -Xss1m (or a bigger parameter for the maximum stack size). The default value is 512 kByte (SUN JDK 1.5 – don’t know if it varies between vendors … Read more

Why is the max recursion depth I can reach non-deterministic?

The observed behavior is affected by the HotSpot optimizer, however it is not the only cause. When I run the following code public static void main(String[] argv) { System.out.println(System.getProperty(“java.version”)); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); } static int countDepth() { try { return 1+countDepth(); } catch(StackOverflowError err) { return 0; } } with JIT … Read more

How do I prevent and/or handle a StackOverflowException?

From Microsoft: Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a … Read more

C# catch a stack overflow exception

Starting with 2.0 a StackOverflow Exception can only be caught in the following circumstances. The CLR is being run in a hosted environment* where the host specifically allows for StackOverflow exceptions to be handled The stackoverflow exception is thrown by user code and not due to an actual stack overflow situation (Reference) *“hosted environment” as … Read more