Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
Linux下一个进程可以创建的最大线程数是多少?
如何(如果可能的话)修改这个值?
当前回答
说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/
其他回答
@dragosrsupercool
Linux不使用虚拟内存来计算线程的最大值,而是使用安装在系统上的物理ram
max_threads = totalram_pages / (8 * 8192 / 4096);
http://kavassalis.com/2011/03/linux-and-the-maximum-number-of-processes-threads/
内核/ fork.c
/* The default maximum number of threads is set to a safe
* value: the thread structures can take up at most half
* of memory.
*/
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
所以thread max在每个系统之间是不同的,因为安装的ram可以来自不同的大小,我知道Linux不需要增加虚拟内存,因为在32位上我们有3 GB的用户空间和1 GB的内核,在64位上我们有128 TB的虚拟内存,这发生在Solaris上,如果你想增加虚拟内存,你需要增加交换空间。
线程数限制:
$ cat /proc/sys/kernel/threads-max
计算方法:
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
和: x86_64页大小(PAGE_SIZE)为4K; 像所有其他体系结构一样,x86_64为每个活动线程都有一个内核堆栈。这些线程栈是THREAD_SIZE (2*PAGE_SIZE)大;
备忘录:
cat /proc/zoneinfo | grep spanned | awk '{totalpages=totalpages+$2} END {print totalpages}';
因此,实际上这个数字与线程内存堆栈大小的限制(ulimit -s)无关。
注:线程内存堆栈限制是10M在我的rhel虚拟机,1.5G内存,这个虚拟机只能提供150个线程?
在实际应用中,限制通常由堆栈空间决定。如果每个线程获得1MB的堆栈(我不记得这是否是Linux上的默认值),那么32位系统将在3000个线程后耗尽地址空间(假设最后gb留给内核)。
但是,如果使用几十个以上的线程,您很可能会遇到糟糕的性能。迟早,您会得到太多的上下文切换开销、调度程序开销等等。(创建大量线程只会消耗大量内存。但是很多有实际工作要做的线程会减慢你的速度,因为它们在争夺可用的CPU时间)
你在做什么,这个极限甚至是相关的?
要检索它:
cat /proc/sys/kernel/threads-max
设置方法:
echo 123456789 | sudo tee -a /proc/sys/kernel/threads-max
123456789 = #线程
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.