/* * Determine the maximum depth of a recursive function in Java * @author gtowell * Created: July 21, 2021 */ public class RecursionTest { // javac RecursionTest.java // java RecursionTest > oo 2> ooo // thenlook on ooo for Exception // my machine 9843 // java -cp . -Xss4m RecursionTest 2> ooo // my machine 42603 private static void recurse(int i) { try { System.err.println(i); recurse(i + 1); } catch (Error e) { System.out.print("Recursion depth on this system is " + i + "."); } } private int cc = 0; public void ccrecurse() { try { System.out.println(cc++); ccrecurse(); } catch (Error e) { System.err.print("Recursion depth on this system is " + cc + "."); } } public void nsrecurse(int i) { try { System.err.println(i); nsrecurse(i + 1); } catch (Error e) { System.out.print("Recursion depth on this system is " + i + "."); } } public static void main(String[] args) { //recurse(0); (new RecursionTest()).nsrecurse(0); //(new RecursionTest()).ccrecurse(); } }