CreateThread之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.
1.线程或者光纤接受自己的栈空间
2.线程或光纤的栈空间由两部分组成:保留内存和初始提交内存
3.保留内存是由总栈在虚拟内存中分配的;所以,保留内存大小在虚拟地址范围里。
4.初始提交页在未被引用时,不占物理内存;但是,它们会从系统总提交范围里移除页空间,系统总提交范围的大小为页文件大小加上物理内存。
5.系统会根据需要从保留的堆栈内存中提交其他页面,直到堆栈大小达到保留大小减去一页(用作防护页面以防止堆栈溢出)或系统内存太低以至于操作失败。
A stack is freed when its thread exits. It is not freed if the thread is terminated by another thread.
1.线程退出后,栈会被释放。
2.线程被其它的线程结束,其栈空间不会被释放。
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.
1.编译器使用的默认保留栈空间大小事1MB。
2.自定义保留栈空间大小,需要在模块定义文件中使用STACKSIZE声明。操作系统会根据系统的分配粒度进行最接近倍数的四舍五入(通常系统的分配粒度为64KB)。可以使用GetSystemInfo函数获取系统的分配粒度值。
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.
1.可以通过函数的dwStackSize参数来修改初始提交栈空间。
2.如果初始提交栈空间比默认的预留大小要大或者等于预留大小,则保留大小为此新提交大小四舍五入到最接近的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.

浙公网安备 33010602011771号