WINDOWS操作系统中可以允许最大的线程数(线程栈预留1M空间)(56篇Windows博客值得一看)
默认情况下,一个线程的栈要预留1M的内存空间
而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程
但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小。
你也可以通过连接时修改默认栈大小,将其改的比较小,这样就可以多开一些线程。
如将默认栈的大小改成512K,这样理论上最多就可以开4096个线程。
即使物理内存再大,一个进程中可以起的线程总要受到2GB这个内存空间的限制。
比方说你的机器装了64GB物理内存,但每个进程的内存空间还是4GB,其中用户态可用的还是2GB。
如果是同一台机器内的话,能起多少线程也是受内存限制的。每个线程对象都要站用非页面内存,而非页面内存也是有限的,当非页面内存被耗尽时,也就无法创建线程了。
如果物理内存非常大,同一台机器内可以跑的线程数目的限制值会越来越大。
在Windows下写个程序,一个进程Fork出2000个左右线程就会异常退出了,为什么?
这个问题的产生是因为windows32位系统,一个进程所能使用的最大虚拟内存为2G,而一个线程的默认线程栈StackSize为1024K(1M),这样当线程数量逼近2000时,2000*1024K=2G(大约),内存资源就相当于耗尽。
MSDN原文:
“The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,028 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.”
如何突破2000个限制?
可以通过修改CreateThread参数来缩小线程栈StackSize,例如
#define MAX_THREADS 50000
DWORD WINAPI ThreadProc( LPVOID lpParam ){
while(1){
Sleep(100000);
}
return 0;
}
int main() {
DWORD dwThreadId[MAX_THREADS];
HANDLE hThread[MAX_THREADS];
for(int i = 0; i < MAX_THREADS; ++i)
{
hThread[i] = CreateThread(0, 64, ThreadProc, 0, STACK_SIZE_PARAM_IS_A_RESERVATION, &dwThreadId[i]);
if(0 == hThread[i])
{
DWORD e = GetLastError();
printf("%d\r\n",e);
break;
}
}
ThreadProc(0);
}
服务器端程序设计
如果你的服务器端程序设计成:来一个client连接请求则创建一个线程,那么就会存在2000个限制(在硬件内存和CPU个数一定的情况下)。建议如下:
The "one thread per client" model is well-known not to scale beyond a dozen clients or so. If you're going to be handling more than that many clients simultaneously, you should move to a model where instead of dedicating a thread to a client, you instead allocate an object. (Someday I'll muse on the duality between threads and objects.) Windows provides I/O completion ports and a thread pool to help you convert from a thread-based model to a work-item-based model.
1. Serve many clients with each thread, and use nonblocking I/O and level-triggeredreadiness notification
2. Serve many clients with each thread, and use nonblocking I/O and readiness changenotification
3. Serve many clients with each server thread, and use asynchronous I/O
http://www.cnblogs.com/lancidie/archive/2011/12/15/2289337.html
WINDOWS操作系统中可以允许最大的线程数(线程栈预留1M空间)(56篇Windows博客值得一看)的更多相关文章
- windows操作系统中安装、启动和卸载memcached
今天总结一下如何在Windows操作系统中安装.启动和卸载memcached:下载地址: http://download.csdn.net/download/wangshuxuncom/8249501 ...
- 在Windows操作系统中,如何终止占有的8080端口的tomcat进程
在Windows操作系统中,我们在启动一个tomcat服务器时,经常会发现8080端口已经被占用的错误,而我们又不知道如何停止这个tomcat服务器. 本文将通过命令来强行终止这个已经运行的tomca ...
- CVE-2019-0797漏洞:Windows操作系统中的新零日在攻击中被利用
https://securelist.com/cve-2019-0797-zero-day-vulnerability/89885/ 前言 在2019年2月,卡巴实验室的自动漏洞防护(AEP)系统检测 ...
- Ant—怎样Windows操作系统中搭建Apache Ant环境
介绍一下怎样在Windows操作系统中搭建Apache Ant环境: 一.下载Apache Ant压缩文件:http://download.csdn.net/detail/wangshuxuncom/ ...
- 在windows操作系统中,查询端口占用和清除端口占用的程序
一.在windows操作系统中,查询端口占用和清除端口占用的程序 提升权限后用:netstat -b或用 1.查询端口占用的进程ID 点击"开始"-->"运行&qu ...
- Git—怎样Windows操作系统中安装Git
介绍一下怎样在Windows操作系统中安装Git: 一.下载Git安装压缩文件:http://download.csdn.net/detail/wangshuxuncom/8035045 二.解压该压 ...
- [转]在 Windows 操作系统中的已知安全标识符(Sid security identifiers)
安全标识符 (SID) 是用于标识安全主体或安全组在 Windows 操作系统中的可变长度的唯一值.常用 Sid 的 Sid 标识普通用户的一组或通用组.跨所有操作系统,它们的值保持不变. 此信息可用 ...
- 在Windows操作系统中安装MongoDB
如何在Windows操作系统中安装MongoDB: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 启动Mon ...
- WINDOWS操作系统中可以允许最大的线程数
默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小. ...
随机推荐
- HTML之学习笔记(六)添加链接
html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签 <a href="URL">热点文字 ...
- ORACLE case when then
Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...
- 全面修复IE,注册IE所有dll
全面修复IE,注册IE所有dll 复制,粘贴到文本文档里,保存成.bat文件,双击运行. rundll32.exe advpack.dll /DelNodeRunDLL32 %systemroot%\ ...
- ubuntu14.04+xfce下启用fcitx,使用中文输入法
1. 安装fcitx sudo apt-get install fcitx-pinyin 2.启用fcitx 打开 setting -> Language Support -> Lang ...
- Android Log工具类
import java.text.SimpleDateFormat; import java.util.Date; import android.util.Log; public class LogU ...
- java下拉框,滚动条
package com.soft.test; /** * 下拉列表.下拉框.滚动条的使用 */ import javax.swing.*; import java.awt.*; public clas ...
- The Use of Aliases in ElasticSearch
http://paulsabou.com/blog/2012/04/15/the-use-of-aliases-in-elasticsearch/ https://github.com/taskrab ...
- Qt监测光驱变化(使用WM_DEVICECHANGE)
xxx.h protected: bool winEvent(MSG *msg,long * result); xxx.cpp bool CBlurayTranscoderDlg::winEvent( ...
- RIA Service 的 SOAP EndPoint
原文 www.cyqdata.com/cnblogs/article-detail-39983-english 越来越多的朋友都在使用Silverlight开发应用程序,其中我们常用的还会有一个特殊的 ...
- [置顶] C#扩展方法 扩你所需
通过前面的学习,了解到:使用扩展方法,可以向现有类型“添加”方法.本文将使用扩展方法来对系统类型,自定义类型及接口进行方法扩展,一睹扩展方法的风采. 1.使用扩展方法来扩展系统类型 String是c# ...