You have a chance to face 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, this OutOfMemoryError will be thrown. The exact limit for native threads is very platform-dependent thus we recommend to find out those limits by running a test similar to the below example. But, in general, the situation causing java.lang.OutOfMemoryError: Unable to create new native thread goes through the following phases:
- A new Java thread is requested by an application running inside the JVM
- JVM native code proxies the request to create a new native thread to the OS
- The OS tries to create a new native thread which requires memory to be allocated to the thread
- 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 depleted
- The java.lang.OutOfMemoryError: Unable to create new native thread error is thrown.