05 September 2018

What is causing java.lang.OutOfMemoryError: Unable to create new native thread

You can encounter the "java.lang.OutOfMemoryError: Unable to create new native thread"

  • whenever the JVM asks for a new thread from the OS
  • Whenever the underlying OS cannot allocate a new native thread
  • The exact limit for native threads is very platform-dependent.
It is recommend to find out those limits by running a test similar to the below.

In general, the situation causing java.lang.OutOfMemoryError: Unable to create new native thread goes through the following phases:
  1. A new Java thread is requested by an application running inside the JVM
  2. JVM native code proxies the request to create a new native thread to the OS
  3. The OS tries to create a new native thread which requires memory to be allocated to the thread
  4. The OS will refuse native memory allocation either because the 32-bit Java process size has depleted its memory address space – e.g. (2-4) GB process size limit has been hit – or the virtual memory of the OS has been fully exhausted.
 Solution:
1) Reduce the amount of memory allocated to each thread through the use of the -Xss jvm option
reducing the stack size using -Xss (e.g “-Xss64kb” ) start with 64k, try the application, then if it doesn’t work (it will fail with a Java.lang.StackOverFlowError),increase the stack to 128k, then 256k, and so on.
2) Determine the problem with the application that requires that so many threads be opened and redesign to prevent so many threads from being spawned.
use "ulimit -a" to check the "max user processes" and "open files" and change the value to a bigger value.

Reference: Oracle Doc ID 2232666.1



No comments:

Post a comment