4

My goal is to come up with figure of max threads which can run in parallel. I was pointed to many links by Google, where they give simple math by dividing the RAM/StackSize. In 64 bit Linux, we have thread stack size defined as 10 MB(ulimit -s = 10240kb) and RAM was 4GB, leaving 1 GB for OS and going with this math I can have ~300 threads or so but small test application which I wrote goes upto ~32297 and then gives out of memory error.

I tried different values with -Xss but these values hardly have any effect on thread count, it remains same as ~32297).

This gave me an impression that stack size is variable and decided by OS and goes upto max defined by us whenever needed, but wherever I read, they size stack size is static

What exactly I'm missing here?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Gaurav
  • 41
  • 1
  • 1
  • 2
  • 1
    You might be limited by the kernel as [mentioned here](http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux) – Joachim Sauer Aug 03 '11 at 07:36
  • 3
    You can have as many threads as are your processors or cores to run truly in parallel. Otherwise the JVM scheduler take liberties to interleave them. – dimitrisli Aug 03 '11 at 07:38
  • @Joachim : I read this post, thanks..Can you please suggest what might be deciding factor behind threads-max variable whose value is 81920 is my case and stack size is 10 MB and RAM is 4 GB. Not able to do math to get these numbers – Gaurav Aug 03 '11 at 08:42
  • http://stackoverflow.com/questions/34452302/how-to-increase-maximum-number-of-jvm-threads-linux-64bit - Following all these checks solved the problem for me. – javaPhobic Jul 01 '16 at 01:18

5 Answers5

5

Try checking/changing linux maximum stack size using

ulimit -s

Also check for linux threads limit

cat /proc/sys/kernel/threads-max  
a--
  • 753
  • 6
  • 15
  • 1
    cat /proc/sys/kernel/threads-max is 81920 . I want to know the relation b/w ulimit -s and threads-max, will threads-max value gets updated if I change the stack size? – Gaurav Aug 03 '11 at 08:20
3

I have also found a limit of about 32K for thread in Java. If you have this many threads, its usually a better idea to use a different approach. On my machine 32K thread doing while(true) Thread.sleep(1000) will consume 3 cores just context switching.

Java: What is the limit to the number of threads you can create?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • In the link you shared, they mention on 64 bit with stack size of 128K, limit was 32,072 and with stack size of 512K limit is again 32,072. Do you know why stack size is not having any impact on number of threads which can be created? – Gaurav Aug 04 '11 at 05:35
3

Linux implements max number of threads per process indirectly!!

number of threads = total virtual memory / (stack size*1024*1024)

Thus, the number of threads per process can be increased by increasing total virtual memory or by decreasing stack size. But, decreasing stack size too much can lead to code failure due to stack overflow while max virtual memory is equals to the swap memory.

Check you machine:

Total Virtual Memory: ulimit -v (default is unlimited, thus you need to increase swap memory to increase this)

Total Stack Size: ulimit -s (default is 8Mb)

Command to increase these values:

ulimit -s newvalue

ulimit -v newvalue

*Replace new value with the value you want to put as limit.

References:

http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/

codersofthedark
  • 9,183
  • 8
  • 45
  • 70
  • "ulimit -v" shows the default value "unlimited". But, on my 12 GB RAM Ubuntu VM, the system calculates the kernel parameter threads-max as 95k. Please clarify how is the threads-max calculated even though "ulimit -v" shows "unlimited"? – Mohan Oct 19 '22 at 07:30
  • since, we are multiplying by 1024*1024, I assume that virtual memory should be given in GBs. Please confirm – Mohan Oct 19 '22 at 07:32
0

It is because of the pid_max kernel variable that is by default 32768, but for 64 bit systems may be increased up to 4 milions. Explanation is simple 1 thread = 1 process that will have 1 PID (process ID) so no more pids, no more threads.

0

What you've read is only valid in 32 bit architecture when the limit is the address space (2^32). You have effectively something like that: Xmx + MaxPermSize + (Xss * number of threads) < Max address space OS allow for user process. Depending on the OS and physical hardware you've something like 3Go like you said. But this has nothing to do with RAM.

For 64 bit architecture, you address space won't be the limitation (2^64). You should look at OS limitation like someone has told above.

Sylvain
  • 446
  • 4
  • 6
  • Fine, the question which is still unanswered is why max thread count reamin same even on changing the stack size to very large or very small value. Does OS choose the stack size dynamically? – Gaurav Aug 04 '11 at 06:08