Skip to content

Instantly share code, notes, and snippets.

@ubergeek42
Last active November 3, 2016 20:10
Show Gist options
  • Save ubergeek42/d721b226c01594ce5a3a61cd065d9822 to your computer and use it in GitHub Desktop.
Save ubergeek42/d721b226c01594ce5a3a61cd065d9822 to your computer and use it in GitHub Desktop.
public class StackTest implements Runnable {
public static long level = 0;
public static long stacksize = 1<<28;
public static long num(int n) {
level++;
return n==0 ? 0 : 1+num(n-1);
}
public void run() {
level = 0;
try {
int depth = 1<<23;
System.out.println("Recursion depth set to: " + depth);
System.out.println(num(depth));
} catch (StackOverflowError e) {
System.out.println(" Stack overflow occurred");
System.out.println(" Stack Level: " + level);
System.out.println(" Estimated Stack Size: " + ((level*16)/(1024)) + "k");
}
}
public static void main(String[] args) {
System.out.println("Testing with thread + stacksize=" + stacksize);
Thread t = new Thread(null, new StackTest(), "main", stacksize);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Testing without thread: ");
new StackTest().run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment