默认情况下,一个线程的栈要预留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博客值得一看)的更多相关文章

  1. windows操作系统中安装、启动和卸载memcached

    今天总结一下如何在Windows操作系统中安装.启动和卸载memcached:下载地址: http://download.csdn.net/download/wangshuxuncom/8249501 ...

  2. 在Windows操作系统中,如何终止占有的8080端口的tomcat进程

    在Windows操作系统中,我们在启动一个tomcat服务器时,经常会发现8080端口已经被占用的错误,而我们又不知道如何停止这个tomcat服务器. 本文将通过命令来强行终止这个已经运行的tomca ...

  3. CVE-2019-0797漏洞:Windows操作系统中的新零日在攻击中被利用

    https://securelist.com/cve-2019-0797-zero-day-vulnerability/89885/ 前言 在2019年2月,卡巴实验室的自动漏洞防护(AEP)系统检测 ...

  4. Ant—怎样Windows操作系统中搭建Apache Ant环境

    介绍一下怎样在Windows操作系统中搭建Apache Ant环境: 一.下载Apache Ant压缩文件:http://download.csdn.net/detail/wangshuxuncom/ ...

  5. 在windows操作系统中,查询端口占用和清除端口占用的程序

    一.在windows操作系统中,查询端口占用和清除端口占用的程序 提升权限后用:netstat -b或用 1.查询端口占用的进程ID 点击"开始"-->"运行&qu ...

  6. Git—怎样Windows操作系统中安装Git

    介绍一下怎样在Windows操作系统中安装Git: 一.下载Git安装压缩文件:http://download.csdn.net/detail/wangshuxuncom/8035045 二.解压该压 ...

  7. [转]在 Windows 操作系统中的已知安全标识符(Sid security identifiers)

    安全标识符 (SID) 是用于标识安全主体或安全组在 Windows 操作系统中的可变长度的唯一值.常用 Sid 的 Sid 标识普通用户的一组或通用组.跨所有操作系统,它们的值保持不变. 此信息可用 ...

  8. 在Windows操作系统中安装MongoDB

    如何在Windows操作系统中安装MongoDB: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 启动Mon ...

  9. WINDOWS操作系统中可以允许最大的线程数

      默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小.  ...

随机推荐

  1. JavaScript之<noscript>标签简介

    早期浏览器都面临一个特殊的问题,即当浏览器不支持JavaScript时如何让页面平稳的退化.对这个问题的终极方案就是创造一个<noscript>元素,用以在不支持或支持但禁用了JavaSc ...

  2. GWT工程 —— HostedMode(宿主模式下调试) 所有的运行命令

    Unknown argument: -helpGoogle Web Toolkit 1.7.0HostedMode [-noserver] [-port port-number | "aut ...

  3. iOS 点转成字符串,再字符串转换成点

    CGPointFromString(<#NSString *string#>) NSStringFromCGPoint(<#CGPoint point#>)

  4. JDK源码学习--String篇(三) 存储篇

    在进一步解读String类时,先了解下内存分配和数据存储的. 数据存储 1.寄存器:最快的存储区,位于处理器的内部.由于寄存器的数量有限,所以寄存器是按需分配. 2.堆栈:位于RAM中,但是通过堆栈指 ...

  5. mysql之数据库特性认识

    最近面试经常被面试官问道关于数据库方面的知识,于是总结一下面试官问的题以及自己对数据库的认识 1.之前百度面试官问了我一个特别基础的sql问题:如何清除表的所有记录,以前在学校做项目开发的时候有使用过 ...

  6. python学习之 dictionary 、list、tuple操作

    python 内置类型数据 有dictionary(字典).list(列表)和tuple(元组) 一.Dictionary Dictionary 是 Python 的内置数据类型之一,它定义了键和值之 ...

  7. install tool

    # 查看静态态依赖库 ldd ./nginx #查看安装了哪些模块 ./nginx -V

  8. 拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)

    本文从今天开始,我要做的就是不断的更新,不断的披露ExtJS 3.4的BUG并修复它.需要注意的是版本为3.4而不是4.0,因为4.0改动和变化比较大,所以不要对号入座. 嘿嘿,本人不怎么写东西,不过 ...

  9. 类 BufferedReader

    以前学习的时候也没有太在意,在项目中使用到了才发现呵呵 1.读取一个txt文件,方法很多种我使用了字符流来读取(为了方便) FileReader fr = new FileReader("f ...

  10. Angular form

    参考 http://blog.xebia.com/2013/10/15/angularjs-validating-radio-buttons/ http://stackoverflow.com/que ...