Created
October 13, 2011 14:15
-
-
Save galak-fyyar/1284314 to your computer and use it in GitHub Desktop.
Crash test for JVM that creates unlimited number of threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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