Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
当前回答
Yes, to increase the threads number you need to increase the virtual memory or decrease the stack size. In Raspberry Pi I didn’t find a way to increase the virtual memory, if a decrease the stack size from default 8MB to 1MB It is possibly get more than 1000 threads per process but decrease the stack size with the “ulimit -s” command make this for all threads. So, my solution was use “pthread_t” instance “thread class” because the pthread_t let me set the stack size per each thread. Finally, I am available to archive more than 1000 threads per process in Raspberry Pi each one with 1MB of stack.
其他回答
可以通过以下命令查看当前值- 猫/proc/sys/kernel/threads-max
您还可以像这样设置值
Echo 100500 > /proc/sys/kernel/threads-max
您设置的值将根据可用的RAM页进行检查。如果线程结构占用可用RAM页面的1/8以上,则thread-max将相应降低。
使用nbio 非阻塞i / o 库之类的,如果你需要更多的线程来做I/O调用那个块
说LINUX没有每个进程单独的线程限制是错误的。
Linux间接实现了每个进程最大线程数!!
number of threads = total virtual memory / (stack size*1024*1024)
因此,可以通过增加总虚拟内存或减小堆栈大小来增加每个进程的线程数。但是,当最大虚拟内存等于交换内存时,过多地减小堆栈大小会导致代码失败,因为堆栈溢出。
检查你的机器:
总虚拟内存:ulimit -v(默认值是无限的,因此您需要增加交换内存来增加它)
总堆栈大小:ulimit -s(默认为8Mb)
命令来增加这些值。
ulimit -s newvalue
ulimit -v newvalue
*将新值替换为您想要放置的限制值。
引用:
http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/
取决于您的系统,只需编写一个示例程序[通过在循环中创建进程],并使用ps axo pid,ppid,rss,vsz,nlwp,cmd检查。当它不能再创建线程时,检查nlwp计数[nlwp是线程数],瞧,你得到了你的傻瓜证明的答案,而不是通过书籍
我认为我们错过了另一个限制,它也会阻止新线程的创建,这是内核。pid_max极限。
root@myhost:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.7 LTS
Release: 16.04
Codename: xenial
root@myhost:~# uname -a
Linux myhost 4.4.0-190-generic #220-Ubuntu SMP Fri Aug 28 23:02:15 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
我发现至少在我的系统中,这个阈值核。Pid_max是32768。当我启动任何简单的JVM进程时,它会报告如下错误:
java/jstack/jstat ...
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Cannot create GC thread. Out of system resources.
# An error report file with more information is saved as:
# /root/hs_err_pid1390.log
检查内存是否充足。
root@lascorehadoop-15a32:~# free -mh
total used free shared buff/cache available
Mem: 125G 11G 41G 1.2G 72G 111G
Swap: 0B 0B 0B
检查系统线程:
~# ps -eLf|wc -l
31506
但是我用ulimit检查系统限制:
root@myhost:~# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 515471
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 98000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 515471
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
从ulimit输出中,我们可以看到当前线程数远远小于最大用户进程限制。
实际上,所达到的极限是kernel.pid_max
非常容易检查和调整: https://www.cyberciti.biz/tips/howto-linux-increase-pid-limits.html