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!

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: