by BehindJava

How many threads can a Java VM support

Home » java » How many threads can a Java VM support

This is quick tutorial on How many threads can a Java VM support? Does this vary by vendor? by operating system? other factors?

This depends on the CPU you’re using, on the OS, on what other processes are doing, on what Java release you’re using, and other factors. I’ve seen a Windows server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.

My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.

Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.

The absolute theoretical maximum is generally a process’s user address space divided by the thread stack size (though in reality, if all your memory is reserved for thread stacks, you won’t have a working program…).

So under 32-bit Windows, for example, where each process has a user address space of 2GB, giving each thread a 128K stack size, you’d expect an absolute maximum of 16384 threads (=210241024 / 128). In practice, I find I can start up about 13,000 under XP.

Then, I think you’re essentially into whether (a) you can manage juggling that many threads in your code and not do obviously silly things (such as making them all wait on the same object then calling notifyAll()…), and (b) whether the operating system can. In principle, the answer to (b) is “yes” if the answer to (a) is also “yes”.

Incidentally, you can specify the stack size in the constructor of the Thread; you don’t need to (and probably shouldn’t) mess about with VM parameters for this.