java.lang.OutOfMemoryError: unable to create new native thread
If you see this error, then there’s most likely an issue with the thread handling in your application. I faced this issue in one of our apps, while deploying it as a WAR deployable in JBoss. This is how we debugged the issue:
The same app was running as a stand-alone and was working fine without any notable issues over a long period of time. Googling around for a day, I tried diff solutions, but all in vain. Finally, I was reading an article, where it was mentioned that there’s a OS-level limit to the no of threads a process can create (or probably actively use).
I tried to find the count of active threads for this app (the given commands will work only in linux/unix machines).
ps -ef|grep <AppName>
gave me the Process ID.
ps uH p <ProcessID> | wc -l
gave me the active thread count. What was shocking is, that app was using whopping 4200+ threads at that time, and that kept on increasing!
When we started debugging the code, we found that, at one point, we were creating lots of Timer instances, whereas a single timer would have done the job. Fixed that show-stopper, and then when we checked again, our app was using just 24 threads!