The maximum number of threads that can be opened on a single host

Source: Internet
Author: User

Source of the problem

Recently in the server pressure and concurrency test, because it is the C/S model, so you need to impersonate the client, need to run a large number of threads to simulate concurrency, and then encountered this problem.

Another reason is that I created a thread pool in the server, I would like to know the maximum number of thread pool, so as to better make the thread pool dynamic increment and decrement method, so also did the same test.

First analysis of the Linux system

For 32-bit systems, the theoretical range of addressing is 4G (2^32), but the actual system also reserves the system space for its own management, in general, the true maximum addressable range is less than 4G. For Linux, a process has a user space of 0~0xbfffffff, a total of 3G, the other 1 g is the kernel space, Windows to 0x7fffffff, only 2 G. (The addressing range of the 32-bit system can also be extended to apply certain extension criteria, such as PAE, to support up to 16G to 64G.) But the situation is not in my scope of discussion).

A Linux process accesses up to 3G of memory in the user state, and each thread must occupy a specific stack size (stack size), which causes the number of threads created by the 32-bit system not to exceed n=3g/stacksize
Note: Stack size can be viewed with ulimit-s, in K, and this size can be set.

My system is ubuntu12.04, this size is 8192k

so roughly the last user process is most about creating 360 or so of threads.

Create Thread 303
Can ' t create thread:cannot allocate memory

Then consider the system

Because there are 64-bit systems, there is no limit to the addressing range, so theoretically, the number of threads that can be created should be almost limitless, unless other conditions are limited.

However, in my actual test, there was an error when the number of created threads reached about 7500. But the reason has not been able to find.
Create thread 7545
Can ' t create Thread:resource temporarily unavailable

How to stacks the size of a reroute

1. Use the ulimit-s size command to temporarily set the thread stack size to size, in K
2. Use pthread_attr_setstacksize to change the thread stack size in the program, in bytes
3. When running the program, whether to run as root, you can avoid the limited resources

The method test of increasing the number of threads under the Linux

1. Using ulimit-s, change the thread stack size to 1024k, the maximum number of creation threads is still 7500, there is no change, indicating that the 64-bit system stack space is no longer the limit.
2. Run the program as root to discover that the thread can run at a level of 1.5w and that it does have high permissions and is limited in size, but at risk.
3. Using Ulimit-s to change the size of the thread stack and then run with root, the result is the same as 2, stating that line stacks space is not a limiting factor.

Test methods for server concurrency

1. First the client program is written, and a single test is performed, ok!
2. Rewrite the client program into multi-threaded, one client running 100 threads (thread is 1 program modification)
3.shell scripts run 2 of clients in parallel 100 times, then you can create 100*100 concurrency.
4. One machine can open a limited number of threads, considering the simultaneous operation of n units, then the concurrency test can be n*100*100.
5. If the client program is not a long connection, you can also let the client thread run again and again!

The maximum number of threads that can be opened on a single host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.