It's been a long time since I shared 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. Web Server 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
Here is sample stack trace:
Exception in thread "http-bio-8506-exec-106"
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:691)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:992)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
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.
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.
That's all about how to solve java.lang.OutOfMemoryError: unable to create new native thread in Java application. Like other memory related error this one also comes due to resource exhaustion, this time threads as every machine and JVM has limit on how many threads your application can create. Though with new virtual thread, you can minimize the chances of this error by using virtual thread instead of physical thread.
Other Related Error and Exception Tutorials for Java Programmers
Thanks for reading this article so far. If you like this troubleshooting guide and my explanation of java.lang.OutOfMemoryError: unable to create new native thread then please share it with your friends and colleagues. If you have any questions or feedback, please drop a comment.
- How to solve java.sql.BatchUpdateException: String or binary data would be truncated (guide)
- How to avoid ConcurrentModificationException in Java? (tutorial)
- How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
- 5 Reasons of NoClassDefFoundError in Java? (tutorial)
- java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener (solution)
- How to solve "could not create the Java virtual machine" error in Java? (solution)
- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application [solution]
- How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
- How to solve "variable might not have initialized" compile-time error in Java? (answer)
- Cause of "java.lang.SecurityException: Missing required Permissions manifest attribute in main jar" [solution]
- How to fix "illegal start of expression" compile time error in Java? (tutorial)
- How to connect to MySQL database in Java? (tutorial)
- How to fix "Error: Could not find or load main class" in Eclipse? (guide)
- How to fix 'javac' is not recognized as an internal or external command (solution)
- java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
- java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)
- java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
- How to deal with "No JVM installation found, please install 64-bit JDK" error? (solution)
- How to fix unable to find certification path error in Java SSL? (guide)
- Common reasons for java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
- 10 common reasons for java.lang.NumberFormatException in Java? (tutorial)
- Fixing java.lang.unsupportedclassversionerror unsupported major.minor version 50.0 (solution)
- Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
- How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? (solution)
Thanks for reading this article so far. If you like this troubleshooting guide and my explanation of java.lang.OutOfMemoryError: unable to create new native thread then please share it with your friends and colleagues. If you have any questions or feedback, please drop a comment.
No comments:
Post a Comment