Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

How to fix java.lang.OutOfMemoryError: unable to create new native thread in Java [Solution]

It's been a long time since I write any Java Error or Exception troubleshooting tutorial so here we are. In this post, we will take a look at the "java.lang.OutOfMemoryError: unable to create new native thread" error which is a rate but quite possible in multithreading Java applications. This is another Java OutOfMemoryError that comes when JVM hits the native thread limitation allowed to be open by one process and not able to create any more new threads. Since each Java thread is associated with the OS thread, when OS refuses to provide a new thread, JVM throws OutOfMemoryError. It's rarely you will see common Java applications throwing this error but it's common when multiple application is using the same Java process e.g. WebServer Tomcat and a significant number of threads are allocated due to high load.


Investigation

Check out the maximum number of processes per user in Linux by using ulimit - a command and then find out when your application died, what was the number of active threads.

Solution:

1) If you are getting "java.lang.OutOfMemoryError: unable to create new native thread" in Tomcat or JBoss, it's best to distribute the application among multiple instances.

2) Alternatively you can also increase the max user process in Linux by using the ulimit command, as by default this limit (number of processes per user) is 1024, adding ulimit -u 4096 to the .profile of the user on which your Java program is running. If you do this make sure you create a new shell and start your application to take new settings.

3) StackTrace
https://confluence.atlassian.com/display/JIRAKB/OutOfMemory+Errors+Due+to+Running+Out+of+Native+Thread+Limitation

4) Though you have the option to increase the number of threads per process, there are hardly any applications that require this many threads, especially after the concept of the thread pool and worker thread is popular. Most likely it's a programming bug that is spawning thousands of threads and those threads are getting stuck somehow e.g. locked in deadlock or blocked in IO.

I have seen "java.lang.OutOfMemoryError: Unable to create new native thread" error during a performance test of one Java application, only to realize that it's creating a separate thread each time a message is received and those threads are not existing because they are trying to update the same table in Database which was also blocked.


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



1) if multiple applications run in the same JVM, then yes, they can easily influence each other. It'll be hard to tell which one is misbehaving. Separating the applications into distinct JVMs might be the easiest solution. This is what happens in the case of web servers like Tomcat or JBoss

2) what does the GC overhead limit mean?
GC Overhead limit means GC is exhausted without any success. JVM throws this error if your process spends 98% of total CPU time collecting Garbage and only managed to collect 2% of total heap space.

3) JVM throws this error to prevent your application from soaking up CPU time without doing anything meaningful.

No comments :

Post a Comment