Maximum number of threads per process in Linux?

2018-12-31 06:20发布

What is the maximum number of threads that can be created by a process under Linux?

How (if possible) can this value be modified?

16条回答
后来的你喜欢了谁
2楼-- · 2018-12-31 06:57

To set permanently,

vim /etc/sysctl.conf

and add

kernel.threads-max = "value"
查看更多
泪湿衣
3楼-- · 2018-12-31 07:01

Use nbio non-blocking i/o library or whatever, if you need more threads for doing I/O calls that block

查看更多
看风景的人
4楼-- · 2018-12-31 07:03

To retrieve it:

cat /proc/sys/kernel/threads-max

To set it:

echo 123456789 > /proc/sys/kernel/threads-max

123456789 = # of threads

查看更多
长期被迫恋爱
5楼-- · 2018-12-31 07:03

For anyone looking at this now, on systemd systems (in my case, specifically Ubuntu 16.04) there is another limit enforced by the cgroup pids.max parameter.

This is set to 12,288 by default, and can be overriden in /etc/systemd/logind.conf

Other advice still applies including pids_max, threads-max, max_maps_count, ulimits, etc.

查看更多
孤独寂梦人
6楼-- · 2018-12-31 07:05

Thread count limit:

$ cat /proc/sys/kernel/threads-max 

How it is calculated:

max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);

and: x86_64 page size (PAGE_SIZE) is 4K; Like all other architectures, x86_64 has a kernel stack for every active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big;

for mempages :

cat /proc/zoneinfo | grep spanned | awk '{totalpages=totalpages+$2} END {print totalpages}';

so actually the number is not related with limitation of thread memory stack size (ulimit -s).

P.S: thread memory stack limitation is 10M in my rhel VM, and for 1.5G memory, this VM can only afford 150 threads?

查看更多
忆尘夕之涩
7楼-- · 2018-12-31 07:08

You can see the current value by the following command- cat /proc/sys/kernel/threads-max

You can also set the value like

echo 100500 > /proc/sys/kernel/threads-max

The value you set would be checked against the available RAM pages. If the thread structures occupies more than 1/8th) of the available RAM pages, thread-max would be reduced accordingly.

查看更多
登录 后发表回答