Skip to content

Instantly share code, notes, and snippets.

@abdennebi
Created December 20, 2013 09:09
Show Gist options
  • Save abdennebi/8052274 to your computer and use it in GitHub Desktop.
Save abdennebi/8052274 to your computer and use it in GitHub Desktop.
package test;
public class RunOutOfMemory {
/**
* Results
*
* ==== Environment: 32bit Win 7, 2GB RAM, Java 7 ====
*
* Reducing the default maximum thread stack size allows more of the process' virtual
* memory address space to be used by the Java heap and vice-versa.
*
* Note, when it comes to virtual memory usage consumed by the jvm process, we are
* not including the heap and permanent generation allocation.
*
* Test: 1. ALL VM arguments at default values: OutOfMemory with 3974 threads =
* 1238 MB (320k / thread)
* 2. -Xss1m, rest VM arguments at default values: OutOfMemory with 1211 threads
* = 1211 MB
* 3. -Xss2m, rest VM arguments at default values: OutOfMemory with 533 threads
* = 1066 MB
* 4. -Xss128k, rest VM arguments at default values: OutOfMemory with 10358 threads
* = 1294 MB
*
* "On Windows, the default thread stack size is read from the binary (java.exe). As of
* Java SE 6, this value is 320k in the 32-bit VM and 1024k in the 64-bit VM." oracle.com
*
* ==== Linux ====
*
* NOTE: Pay close attention to your limits configured in /etc/security/limits.conf You
* may want to adjust the nproc setting for your process upwards if you are seeing a
* "cannot create native thread" message
*/
public static void main(String[] pArgs) {
try {
// keep spawning new threads forever
while (true) {
// thread begins execution, JVM calls run method of this new thread
new TestThread().start();
}
} catch (java.lang.OutOfMemoryError e ) {
// when out of memory error is reached, print out the number of
// successful threads spawned and exit
System.out.println(e);
System.out.println(TestThread.CREATE_COUNT);
System.exit(-1);
}
}
static class TestThread extends Thread {
private static int CREATE_COUNT = 0;
public TestThread() {
CREATE_COUNT++;
}
// make the thread wait for eternity after being spawned
public void run() {
this.suspend();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment