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

I saw such a comment

In one place, I saw this problem if you continue to create threads, and instead of calling start (), call run () directly on the thread object. This will cause the stream object to not be dereferenced ... Therefore, after some time, a message that is unable to create a new own stream appears

Sun Java Forums

In my application, at the beginning we plan to use a stream, but later we didn’t decide anymore, so we just call run () instead of start (). Do I need to do a manual GC for the new threadClass (..)?

my tomcat launch setup

-Xms1024m -Xmx1024m -XX:MaxPermSize=450m 
+6
java memory-leaks
source share
3 answers

Why are you creating Thread in the first place?

Instead, your code should implement Runnable .

Then, when you decide that you want to start it in the thread, just create an instance of Thread with Runnable as an argument and call start() on the Thread object.

If instead you just want to run it in the current thread, just call run() on your Runnable .

This has several advantages:

  • you do not include any Thread objects unless you need separate threads.
  • your code is wrapped in Runnable , which comes closer conceptually: you are not writing any special kind of Thread, are you? You just write code that can be run / run.
  • you can easily switch to using Executor , which further abstracts the solution

Last but not least, you avoid potential confusion as to whether a resource of its own flow has been created.

+11
source share

When you call the run () method, a new thread should not be created. And your objects will be collected by the garbage collector if they are not referenced.

Another piece of code can create many threads.

Try using the ThreadPoolExecutor (thread pool) in your code to limit the threads in your application and adjust the thread stream size accordingly for better performance.

You can also check the following to debug your problem: (link from link) There are a few things to do if you encounter this exception.

  • Use the lsof -p PID command (Unix platforms) to find out how many threads are active for this process.
  • Determine if there is a maximum number of threads per process by the operating system. If the limit is too small for the application, try raising the thread limit for each process.
  • Examine the application code to determine if there is code that creates threads or connections (such as LDAP connections) and not destroying them. You can flush Java threads to see if an excess number has been created.
  • If you find that too many connections are opened by the application, make sure that any thread that the application creates. enterprise application (.ear) or web application application (.war) runs under long-term JVM. Just because the application is terminated does not mean that the JVM process is terminating. It is necessary that the application is free of any resources that it allocates. Another solution would be for an application to use a thread pool to manage the required threads.
+4
source share

This link pretty well describes how this error is generated by the JVM: http://javaeesupportpatterns.blogspot.ro/2012/09/outofmemoryerror-unable-to-create-new.html

Basically it is very OS dependent. On RedHat Linux 6.5 (most likely other versions of the distribution / version and kernel) max_threads = max_process x 2.

The maximum number of threads depends on the number of allowed processes. What maximum number of processes depends on the maximum physical memory that you have installed.

If you look in the limits.conf file (on my RHL 6.5 in /etc/security/limits.d/90-nproc.conf). Run file:

 # Default limit for number of user processes to prevent # accidental fork bombs. # See rhbz #432903 for reasoning. * soft nproc **1024** root soft nproc unlimited 

You will see that for users without root, this is 1024 (which means 2048 maximum threads).

To see the maximum number of threads that your user can create, run this command "cat / proc / sys / kernel / threads-max" or "sysctl kernel.threads-max".

To solve a problem like this (at least it worked for me) as root, you will need ncrease of the maximum allowed threads:

echo 10000> / proc / sys / kernel / threads-max

This affects all users and the root. The user must log out and then log back in for the settings to affect.

+1
source share

All Articles