Splunk Completes Acquisition of Plumbr Learn more

The following example creates and starts new threads in a loop. When running the code, operating system limits are reached fast and java.lang.OutOfMemoryError: Unable to create new native thread message is displayed.


while(true){
    new Thread(new Runnable(){
        public void run() {
            try {
                Thread.sleep(10000000);
            } catch(InterruptedException e) { }        
        }    
    }).start();
}

The exact native thread limit is platform-dependent, for example tests on Windows, Linux and Mac OS X reveal that:

  • 64-bit Mac OS X 10.9, Java 1.7.0_45 – JVM dies after #2031 threads have been created
  • 64-bit Ubuntu Linux, Java 1.7.0_45 – JVM dies after #31893 threads have been created
  • 64-bit Windows 7, Java 1.7.0_45 – due to a different thread model used by the OS, this error seems not to be thrown on this particular platform. On thread #250,000 the process was still alive, even though the swap file had grown to 10GB and the application was facing extreme performance issues.

So make sure you know your limits by invoking a small test and find out when the java.lang.OutOfMemoryError: Unable to create new native thread will be triggered