Maximum number of threads in a JVM?

The real question should be not how many threads you can create but how many threads will run efficiently. Too many threads and you will cause thrashing, too few and less computation time.

First, question, how long to live is your thread. Short live threads are hardly worth the effort. Large computations on the other hand make perfect sense.

Second, how much memory will each thread consume. Take the amount of memory each thread requires and divided it by the amount of memory available. You should not create more threads than this.

Thirdly, how many CPUs do you have available. You should not create more threads than CPUs. In fact, you should consider at least one less than the number of threads. On a windows laptop with 4 CPUs, you should never have more than 3 Threads if efficient processing is required.

Finally, what does your thread do. If it reads and writes to a hard drive, then you can have more threads than the number of CPUs since it will have to wait for the device to respond. Consider the following method when deciding the number of threads:

public static int recommendedThreadCount()
{
    int mRtnValue = 0;
    Runtime runtime = Runtime.getRuntime();
    long maxMemory = runtime.maxMemory();
    long mTotalMemory = runtime.totalMemory();
    long freeMemory = runtime.freeMemory();
    int mAvailableProcessors = runtime.availableProcessors();

    long mTotalFreeMemory = freeMemory + (maxMemory - mTotalMemory);
    mRtnValue = (int)(mTotalFreeMemory/4200000000l);

    int mNoOfThreads = mAvailableProcessors-1;
    if(mNoOfThreads < mRtnValue) mRtnValue = mNoOfThreads;

    return mRtnValue;
}

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

The limit, if there is one, will be imposed by the operating system, not the jvm


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.

Tags:

Java