Thread stack size windows 2008




















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. The arguments and the local variables of a function are stored in the stack of the thread.

If you declare a local variable that has a large value, the stack is quickly exhausted. For example, the function in the following code example requires , bytes in the stack to store the array. To avoid using the stack, dynamically allocate the memory. For example, the function in the following code example dynamically allocates the memory. In this code example, the memory is stored in the heap instead of the stack. Therefore, the function does not require , bytes in the stack to store the array.

If a function is called recursively, the stack may be quickly exhausted. A page with no access permission is appended to the top of the stack so that most stack overflows result in sending a SIGSEGV signal to the offending thread.

Thread stacks allocated by the caller are used as is. Generally, you do not need to allocate stack space for threads. The threads library allocates 1 Mbyte of virtual memory for each thread's stack with no swap space reserved. Each thread stack created by the threads library has a red zone. The library creates the red zone by appending a page to the top of a stack to catch stack overflows. The results are the same when the —n switch is specified, because the threads have the same initial stack commitment.

The number of processes that Windows supports obviously must be less than the number of threads, since each process has one thread and a process itself causes additional resource usage.

If the only cost of a process with respect to resident available memory was the kernel-mode thread stack, Testlimit would have been able to create far more than 8, threads on a 2GB system. Dividing the amount of resident memory Testlimit used 1.

Since a bit kernel stack is 24K, that leaves about K unaccounted for. This acts as a guarantee to the process that no matter what, there will enough physical memory available to hold enough data to satisfy its minimum working set.

The remaining roughly 6K is resident available memory charged for additional non-pageable memory allocated to represent a process. A process on bit Windows will use slightly less resident memory because its kernel-mode thread stack is smaller.

As they can for user-mode thread stacks, processes can override their default working set size with the SetProcessWorkingSetSize function. Testlimit supports a —n switch, that when combined with —p, causes child processes of the main Testlimit process to set their working set to the minimum possible, which is 80K.



0コメント

  • 1000 / 1000