java.lang.OutOfMemoryError: unable to create new native thread

In JVM, each thread is allocated separate memory space called "thread stack", which is configured by JVM option "-Xss". The default value depends on OS/JVM. As number of threads increase, memory usage increases and can lead to 'out ofMemoryError'. Remember, the memory for threads is allocated on stack and not on Java Heap.

Solution:

If you are getting this error, then you can try decreasing the memory allocated to heap (-Xmx) and/or decreasing the thread stack memory (-Xss). Increasing -Xmx (32bit systems) will leave less room for threads if it is being used, hence the opposite of the solution.

Remember the memory allocated using '-Xss' is the maximum memory allocated per thread, and usually it's in KB. Also too low -Xss value can cause java.lang.StackOverflowError. If you continue to get the same error, you probably have thread leak, fix the program.

Other OutOfMemoryErrors:

Comments *
Name *
Email
Please Answer this simple math Question *(4 + 8) =

Comments
siddu thoda confusion hai rey mera bhai ...ek bar dhek ki new matter lagavo....
12 Sep 2012
 
Rajesh Nice post. can you please explain the case with 32-bit systems - why would that leave even lesser space for the threads?
6 Nov 2014
 
Mahesh Hi Rajesh, On 32 bit system (e.g. on Windows), the memory available for Java processes is limited to roughly 2GB (although 4GB is addressable). Now out of this you can allocate roughly 1.4 to 1.6GB to your heap, and rest is left for Perm Gen, stack etc. Now if you allocate more for Heap, the less would be available for Stack, where Threads are created. So if you reduce the memory allocated to Heap, more space would be available for Stack which can avoid OutOfMemoryError mentioned above. I ho
7 Nov 2014