Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Last active December 19, 2021 20:50
Show Gist options
  • Save MasterEx/9a1ace1d53e166f6ffb7a2131abd5550 to your computer and use it in GitHub Desktop.
Save MasterEx/9a1ace1d53e166f6ffb7a2131abd5550 to your computer and use it in GitHub Desktop.
"Overflowing the stack for fun" code examples
This gist contains all the code examples included in the blog post "Overflowing the stack for fun".
public class RecursiveArrayTraversal {
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(int i=0; i< arr.length; i++) {
System.out.println(i);
}
}
}
public class RecursiveArrayTraversal {
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
printArray(arr, 0);
}
public static void printArray(int[] arr, int idx) {
if (idx < arr.length) {
System.out.println(arr[idx]);
printArray(arr, ++idx);
}
}
}
import java.util.stream.IntStream;
public class RecursiveArrayTraversalStackOverflow {
public static void main(String[] args) {
int[] arr = IntStream.range(1, 19122).toArray();
printArray(arr, 0);
}
public static void printArray(int[] arr, int idx) {
if (idx < arr.length) {
System.out.println(arr[idx]);
printArray(arr, ++idx);
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class StackExhaustionRecursion {
public static final AtomicInteger counter = new AtomicInteger(0);
public static void recur() {
counter.incrementAndGet();
recur();
}
public static void main(String[] args) {
try {
recur();
} catch (StackOverflowError ex) {
ex.printStackTrace();
System.out.println("Depth: " + counter.get());
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class StackExhaustionRecursionWithLocalParameters {
public static final AtomicInteger counter = new AtomicInteger(0);
public static void recur() {
int a = 5;
int b = 10;
int c = 15;
int d = 20;
int e = 25;
int f = 30;
int g = 35;
int h = 40;
counter.incrementAndGet();
recur();
}
public static void main(String[] args) {
try {
recur();
} catch (StackOverflowError ex) {
ex.printStackTrace();
System.out.println("Depth: " + counter.get());
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class StackExhaustionRecursionWithObjectRef {
public static final AtomicInteger counter = new AtomicInteger(0);
static class ParamObject {
int a = 5;
int b = 10;
int c = 15;
int d = 20;
int e = 25;
int f = 30;
int g = 35;
int h = 40;
}
public static void recur(ParamObject a) {
counter.incrementAndGet();
recur(a);
}
public static void main(String[] args) {
ParamObject po = new ParamObject();
try {
recur(po);
} catch (StackOverflowError ex) {
ex.printStackTrace();
System.out.println("Depth: " + counter.get());
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class StackExhaustionRecursionWithPrimitiveMethodArguments {
public static final AtomicInteger counter = new AtomicInteger(0);
public static void recur(int a, int b, int c, int d, int e, int f, int g, int h) {
counter.incrementAndGet();
recur(a, b, c, d, e, f, g, h);
}
public static void main(String[] args) {
try {
recur(0, 1, 2, 3, 4, 5, 6, 7);
} catch (StackOverflowError ex) {
ex.printStackTrace();
System.out.println("Depth: " + counter.get());
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadCountVsMaxSize {
static AtomicInteger threadCount = new AtomicInteger(1);
public static void main(String[] args) {
try {
for (;;) {
new Thread(() -> {
for (;;) {
try {
Thread.sleep(1000);
} catch (Throwable ex) {
ex.printStackTrace();
System.err.println("This is a sleep exception");
}
}
}).start();
threadCount.incrementAndGet();
}
} catch (Throwable e) {
System.out.println("Thread Count: " + threadCount.incrementAndGet());
e.printStackTrace();
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment