64 位 linux 上的 Java 线程堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6923062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:26:02  来源:igfitidea点击:

Java thread stack size on 64-bit linux

javalinuxmultithreadingjvmstack-size

提问by Gaurav

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.

我的目标是提出可以并行运行的最大线程数。谷歌向我指出了许多链接,他们通过划分 RAM/StackSize 给出了简单的数学计算。在 64 位 Linux 中,我们将线程堆栈大小定义为 10 MB(ulimit -s = 10240kb),RAM 为 4GB,为 OS 留出 1 GB,按照这个数学计算,我可以拥有大约 300 个线程,但是我的小型测试应用程序写到 ~32297 然后给出内存不足错误。

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

我用 -Xss 尝试了不同的值,但这些值对线程数几乎没有任何影响,它仍然与 ~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?

我到底错过了什么?

回答by Sylvain

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.

当限制是地址空间 (2^32) 时,您所阅读的内容仅在 32 位体系结构中有效。您实际上有类似的东西:Xmx + MaxPermSize + (Xss * 线程数) < 操作系统允许用户进程的最大地址空间。根据操作系统和物理硬件,您可以像您说的那样使用 3Go。但这与 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.

对于 64 位架构,您的地址空间不会成为限制 (2^64)。您应该像上面所说的那样查看操作系统限制。

回答by Ankit Gupta

Try checking/changing linux maximum stack size using

尝试使用检查/更改 linux 最大堆栈大小

ulimit -s

Also check for linux threads limit

还要检查linux线程限制

cat /proc/sys/kernel/threads-max  

回答by Peter Lawrey

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 中的线程限制约为 32K。如果您有这么多线程,通常最好使用不同的方法。在我的机器上,32K 线程while(true) Thread.sleep(1000)将消耗 3 个内核,只是上下文切换。

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

Java:您可以创建的线程数限制是多少?

回答by codersofthedark

Linux implements max number of threads per process indirectly!!

Linux 间接实现了每个进程的最大线程数!!

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)

总虚拟内存:(ulimit -v默认是无限的,因此你需要增加交换内存来增加这个)

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

总堆栈大小:(ulimit -s默认为 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/

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

回答by gneagoe

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.

这是因为pid_max 内核变量默认为32768,但对于64 位系统可能会增加到400 万。解释很简单 1 个线程 = 1 个将有 1 个 PID(进程 ID)的进程,所以没有更多的 pid,没有更多的线程。