Linux Commands

Get and Set Max Thread Count in Linux

“The term “process” refers to a currently running program, whereas “thread” refers to a lightweight process. When multiple threads execute within a program by sharing the same memory, it is called multithreading. In a multithreaded process, you can run multiple threads simultaneously because the purpose is to boost performance.

There is a limit to the number of threads in Linux for effective performance. Setting the threads-max kernel parameter ensures that the number of threads per process stays at that limit or below it. If you also want to know how to set the max thread count, please read our guide. In this guide, we will explain how to get and set the max thread count in Linux.”

Get and Set Max Thread Count in Linux

Let’s divide this section into two parts; the first is to get the thread count, and the other is to set the max thread count.

Get Maximum Threads Count

You can control the maximum number of threads using the thread-max kernel parameter. “file /proc/sys/kernel/threads-max” defines this parameter.

Using the “cat” command, you can view this file.

cat /proc/sys/kernel/threads-max

Here, the output 45444 shows the maximum 45444 threads the kernel can execute.

You can also look up the threads-max value with the “sysctl” command.

sudo sysctl -a | grep threads-max

In addition, “vm.max_map_count” and “kernel.pid_max” specify two additional limits. These limits also prevent the creation of new threads during high load.

It specifies the maximum number at which PIDs will wrap around.

cat /proc/sys/kernel/pid_max

Here is the maximum value of the kernel.pid_max value is 4194304. It means the kernel can execute a maximum of 4194304 processes simultaneously.

A process can only have a certain number of virtual memory areas (VMAs) under the “max_map_count” parameter.

cat /proc/sys/vm/max_map_count

A process’s memory-mapped regions are expressed in the output above.

A thread and a process work similarly in the Linux kernel. Therefore, the value limiting the number of processes also indirectly limits the number of threads. For this reason, kernel.pid_max must be greater than the total number of processes and threads together.

Many threads consume more memory for working on the server. “vm.max_map_count” limits the number of threads and virtual memory for those needing this memory to set their private stack.

Another limit on systemd systems is the “cgroup pids.max” parameter, which defaults to 12,288. Sometimes this default resource limit may be too restrictive or not enough.

Alternatively, it may be useful to make specific adjustments to some of systemd’s tasksMax settings. The “UserTasksMax” parameter overrides the default limit in the [login] section of /etc/systemd/logind.conf.

grep -i "^UserTasksMax" /etc/systemd/logind.conf

Just as systemd enforces thread limits for programs run from the login shell, it does the same.

Set Maximum Thread Count

So far, we have looked at the maximum thread count of different parameters and will now see how to set these maximum threads. Various options are available for setting the maximum number of threads per process. The thread number is set here for a specific process.

Using the following command, you can temporarily set the threads-max kernel parameter at runtime.

sudo /bin/su -c "echo 150000 > /proc/sys/kernel/threads-max"

Also, you can set the threads-max kernel parameter permanently by adding kernel.threads-max=<value> to the /etc/sysctl.conf file.

sudo /bin/su -c "sysctl -w kernel.threads-max=170000 >> /etc/sysctl.conf"

Now we set the pid_max parameter to 200000. This means that the kernel can execute up to 200,000 processes simultaneously.

sudo /bin/su -c "echo 200000 > /proc/sys/kernel/pid_max"

You also specify the maximum number of VMAs or Virtual Memory Areas that a process may own with the “max_map_count” parameter.

sudo /bin/su -c "echo 600000 > /proc/sys/vm/max_map_count"

For all users, “UserTasksMax” sets the thread limit and specifies the TasksMax setting on the systemd systems.

sed -i "s/^UserTasksMax/#UserTasksMax/" /etc/systemd/system.conf
echo "UserTasksMax=60000" >> /etc/systemd/system.conf
grep -i "UserTasksMax" /etc/systemd/logind.conf

System parameters also limit the number of threads per process, possibly before memory and the OS become limiting factors. Increasing the number of threads is also possible by reducing the stack size per thread.

Wrapping Up

In this guide, we explained a multithreading or multi-thread process. Next, we count each process’s maximum thread limit and learn about the importance of the maximum number of threads. After taking all this information, we can set the maximum value of threads.

You can set the maximum value of threads in several ways, which we’ve explained in this guide. By seeing and understanding this, you can also set the maximum values ​​of your threads in Linux.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.