Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of threads in a JVM?

Tags:

java

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.

like image 917
Craig Locke Avatar asked Oct 11 '11 13:10

Craig Locke


People also ask

How many threads can a JVM handle?

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.

What is JVM thread count?

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.

How many threads are currently running in the JVM?

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.

Can we create 1000 threads in Java?

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.


2 Answers

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.

  • 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.
  • A Oracle 64 bit JVM will default to 1M stack size per thread.
    • For each gigabyte of memory you would get 1024 threads using the defaults.
  • For Linux only:
    • ulimit -a will give you the configured limits, for user processes and memory
    • You will only get 32k unique PIDs in linux cat /proc/sys/kernel/pid_max - a maximum of 32k processes.
    • You will get only 255k threads cat /proc/sys/kernel/threads-max
like image 68
flob Avatar answered Sep 28 '22 07:09

flob


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.

like image 30
Jay Avatar answered Sep 28 '22 05:09

Jay