ThreadPool growth related important details
ThreadPool growth related important details
When CLR loaded into a process, it creates a ThreadPool. Each CLR has its own ThreadPool which shares between all AppDomains within the CLR. The ThreadPool provides two types of threads: Worker and I/O Completion Port(IOCP).
Worker threads are used for things like processing the Task.Run(...) or ThreadPool.QueueUserWorkItem(...) methods.
IOCP threads are used when asynchronous IO happens, such as when reading from the network.
The thread pool provides new worker or I/O completion threads on demand(without any throttling) until it reaches the "Minimum" setting for each type of thread. By Default, the minimum number of threads is set to the number of processors on a system.
Once the number of existing(busy) threads hits the "minimum" number of threads, the ThreadPool will throttle the rate at which it injects new threads to one thread per 500 ms.
Typically, if your system gets a burst of work needing an IOCP thread, it will process that work quickly. However, if the burst of work is more than the configured "Minimum" setting, there will be some delay in processing some of work as the ThreadPool watis for one of two things to happen:
1.An existing thread becomes free to process the work.
2.No existing thread becomes free for 500ms, so a new thread is created.
Basically, it means that when the number of Busy threads is greater than Min threads, you are likely paying 500ms delay before network traffic is processed by the application. Also, it is important to note that when an existing thread stays idle for longer than 15s(based on what i remember), it will be cleaned up and this cycle of growth and shrinkage can repeat.
Recommendation
Given this information, we strongly recommend that customers set the minimum configuration value for IOCP and WORKER threads to something larger than the default value. We can't give one-size-fits-all guidance. so each customer needs to fine-tune this setting to their specific needs. A good starting place is 200 or 300, then test and tweak as needed.
Minimum threads setting configuration
a. using the ThreadPool.SetMinThreads(...) method in application during starting. this is preferred mechanism.
b. using the minIoThreads or minWorkerThreads configuration setting under the <processModel> configuration element in Machine.config, usually located at %SytemRoot%\Microsoft.NET\Framework\[versionNumber]\CONFIG.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?