Skip to content

Instantly share code, notes, and snippets.

@galak-fyyar
Created October 13, 2011 14:15
Show Gist options
  • Save galak-fyyar/1284314 to your computer and use it in GitHub Desktop.
Save galak-fyyar/1284314 to your computer and use it in GitHub Desktop.
Crash test for JVM that creates unlimited number of threads
import java.util.*;
import java.text.*;
class CrashThread extends Thread {
public static final long BYTES_IN_MEGABYTE = 1024 * 1024;
public static void main(String[] a) throws Throwable {
try {
System.out.println("Maximum possible heap size is " + Runtime.getRuntime().max Memory() / BYTES_IN_MEGABYTE);
Thread thread;
int threadMaxCount = Integer.MAX_VALUE;
System.out.println(threadMaxCount + " threads will be created");
Date now;
DateFormat df = DateFormat.getTimeInstance();
int threadCount = 1;
while (threadCount < threadMaxCount) {
now = new Date();
thread = new CrashThread();
thread.setDaemon(true);
thread.start();
System.out.print(df.format(now) + " launched threed " + threadCount + ". ");
System.out.println("Heap size is " + Runtime.getRuntime().totalMemory() / BYTES_IN_MEGABYTE);
threadCount++;
}
System.out.println("Successful exit");
} catch (Throwable t) {
System.out.println(t);
throw t;
}
}
public void run() {
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted");
Thread.currentThread().interrupt();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment