I had a problem on a test machine running Red Hat Linux (kernel version 2.4.21-37.ELsmp) using Java 1.6 (1.6.0_02 or 1.6.0_04). The problem is that after creating a certain number of threads in one thread group, the operating system does not want or cannot create more.
This seems to be typical of Java creating threads, as the C thread stream program was able to create about 1.5k threads. Also, this does not happen with the Java 1.4 JVM ... it can create threads over 1.4k, although they are obviously handled differently with respect to the OS.
In this case, the number of threads that it disconnects is only 29 threads. This can be verified with a simple Java program that simply creates threads until it receives an error message and then prints the number of threads it created. Mistake:
java.lang.OutOfMemoryError: unable to create new native thread
This does not seem to be affected by such things as the number of threads used by other processes or users, or the total amount of memory that the system uses at that time. JVM settings like Xms, Xmx, and Xss do not seem to change anything (which is expected, given that the problem is with creating threads of your own OS).
The output of "ulimit -a" is as follows:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited
User process limitation does not seem to be a problem. Finding information about what might be wrong is not much, but this post seems to indicate that at least some Red Hat cores limit the process to 300 MB of memory allocated for the stack, and 10 MB per thread for the stack seems to that the problem may be there (although it seems strange and unlikely).
I tried resizing the stack using "ulimit -s" to check this, but any value other than 10240 and the JVM does not start with an error:
Error occurred during initialization of VM
Cannot create VM thread. Out of system resources.
I can get around Linux at all, but I really know little about the system configuration, and I could not find anything specific to deal with this situation. Any ideas on which system settings or JVMs might trigger this would be appreciated.
Editing Running the thread restriction program mentioned by the plinth didn't crash until he tried to create thread 1529.
The problem also did not occur using 1.4 JVM (occurs with JVM 1.6.0_02 and 1.6.0_04, cannot test with 1.5 JVM at the moment).
The code for the stream test that I use is as follows:
public class ThreadTest { public static void main(String[] pArgs) throws Exception { try {
If you run this using the 1.4 JVM, it freezes when it cannot create more threads and requires kill -9 (at least for me).
More Edit:
It turns out that the system that has the problem uses the LinuxThreads streaming model, while the other system that works fine uses the NPTL model.