Thread Stack Size

本文详细介绍了在Windows操作系统中创建新线程或纤维时如何设置栈大小,包括默认栈大小、栈保留与提交的概念、如何通过不同API调整栈大小等。
http://msdn.microsoft.com/en-us/library/ms686774(VS.85).aspx
  MSDN
Thread Stack Size

Each new thread or fiber receives its own stack space consisting of both reserved and initially committed memory. The reserved memory size represents the total stack allocation in virtual memory. As such, the reserved size is limited to the virtual address range. The initially committed pages do not utilize physical memory until they are referenced; however, they do remove pages from the system total commit limit, which is the size of the page file plus the size of the physical memory. The system commits additional pages from the reserved stack memory as they are needed, until either the stack reaches the reserved size minus one page (which is used as a guard page to prevent stack overflow) or the system is so low on memory that the operation fails.

It is best to choose as small a stack size as possible and commit the stack that is needed for the thread or fiber to run reliably. Every page that is reserved for the stack cannot be used for any other purpose.

A stack is freed when its thread exits. It is not freed if the thread is terminated by another thread.

The default size for the reserved and initially committed stack memory is specified in the executable file header. Thread or fiber creation fails if there is not enough memory to reserve or commit the number of bytes requested. The default stack reservation size used by the linker is 1 MB. To specify a different default stack reservation size for all threads and fibers, use the STACKSIZE statement in the module definition (.def) file. The operating system rounds up the specified size to the nearest multiple of the system's allocation granularity (typically 64 KB). To retrieve the allocation granularity of the current system, use the GetSystemInfo function.

To change the initially committed stack space, use the dwStackSize parameter of the CreateThread, CreateRemoteThread, or CreateFiber function. This value is rounded up to the nearest page. Generally, the reserve size is the default reserve size specified in the executable header. However, if the initially committed size specified by dwStackSize is larger than or equal to the default reserve size, the reserve size is this new commit size rounded up to the nearest multiple of 1 MB.

To change the reserved stack size, set the dwCreationFlags parameter of CreateThread or CreateRemoteThread to STACK_SIZE_PARAM_IS_A_RESERVATION and use the dwStackSize parameter. In this case, the initially committed size is the default size specified in the executable header. For fibers, use the dwStackReserveSize parameter of CreateFiberEx. The committed size is specified in the dwStackCommitSize parameter.

The SetThreadStackGuarantee function sets the minimum size of the stack associated with the calling thread or fiber that will be available during any stack overflow exceptions.


Send comments about this topic to Microsoft

Build date: 8/7/2008

### 关于编程上下文中栈大小的理解 在编程环境中,栈(stack)是一种用于存储局部变量、函数参数以及返回地址的数据结构。栈的工作原理遵循后进先出原则(LIFO),即最后被压入栈中的数据最先弹出。 #### 栈溢出(stack overflow) 当程序运行过程中试图分配超出可用空间的内存到栈上时会发生栈溢出错误。这通常发生在递归调用过深或者局部数组过大等情况之下。一旦发生这种情况,可能会导致应用程序崩溃甚至操作系统不稳定[^4]。 对于Python而言,`stack_size()` 是内置模块 `threading` 提供的方法之一,用来获取创建新线程所需的堆栈尺寸[^1]。然而,在C/C++等低级语言中,则可以通过编译器选项或特定平台上的API来调整默认值;例如Windows下的CreateThread API,默认情况下每个线程拥有大约一兆字节(MB)左右的空间作为其工作区的一部分[^2]。 #### 设置进程或线程的栈大小 为了提高性能并防止不必要的资源浪费,开发者可以根据实际需求自定义这些数值: - **Java虚拟机(JVM)** 中可通过命令行参数 `-Xss<size>` 来设定初始线程栈大小; - C/C++项目里可以利用链接阶段标志 `/STACK:size[,commit]` 或者直接修改源码内的 `_beginthreadex/_endthreadex` 函数调用方式实现相同目的; - Python虽然不支持显式改变单个线程的栈容量,但是允许通过环境变量PYTHONTHREADSSTACKSIZE间接影响全局配置[^3]。 值得注意的是,减少默认栈大小确实能够增加可并发执行的最大线程数,但这并不意味着总是应该这样做——因为更小的栈也更容易触发未预见的问题比如内部缓冲区不足引发的应用异常终止等问题。 ```cpp // 修改C++项目的线程栈大小示例 #include <windows.h> ... DWORD dwStackSize = 512 * 1024; // 定义新的栈大小为512KB HANDLE hThread = CreateThread(NULL,dwStackSize,(LPTHREAD_START_ROUTINE)pfnStartAddr,param,0,&dwThreadId); if(hThread != NULL){ CloseHandle(hThread); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值