What are the maximum number of threads which can be maintained by the Java virtual machine?
I did not explain this in my original question, but I am trying to benchmark the JVM and would like to try and see how many threads it can concurrently maintain.
Creating threads in a loop until an exception is thrown is an option, however, I would like to know if there is a better way to do this.
4.2. On Windows machines, there's no limit specified for threads. Thus, we can create as many threads as we want, until our system runs out of available system memory.
A Oracle 32 bit JVM will default to 320kb stack size per thread. For a 32 bit JVM with 2gb of addressable memory this will give you a maximum of 6.5k threads.
You see, besides the main thread which runs the main program, there are 4 other threads - which are core and default threads necessary to run the JVM.
Generally, you shouldn't create too many Thread instances in Java because both JVM and the Operating system have a limit on how many threads you can create in Java. Crossing that limit will result in an error like java. lang.
There will be some limits imposed by your operating system and hardware configuration.
To raise the number of concurrent threads you should lower the default stacksize java -Xss 64k
.
ulimit -a
will give you the configured limits, for user processes and memorycat /proc/sys/kernel/pid_max
- a maximum of 32k processes.cat /proc/sys/kernel/threads-max
Writing a loop that creates new threads until it blows up is the definitive way to find out. You might well see performance degrade terribly before it actually dies.
I don't know if there's any configuration parameter or other built-in limit in the JVM off the top of my head. I've never run into a limit in practice. Of course sooner or later you will run out of memory, maybe some other resource.
I suspect that there is not a limit on number of threads per se, but rather on resources associated with a thread. That is, you might see that you can have 10,000 threads if all of them are running just one small class with a few bytes of data each, but the number drops rapidly when they each have an array of 10 million strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With