Default Stack in Win32-Based ApplicationsLast reviewed: December 20, 1996Article ID: Q97786 |
The information in this article applies to:
SUMMARYBy default, space is reserved for applications in the following manner:
1 megabyte (MB) reserved (total virtual address space for the stack) 1 page committed (total physical memory allocated when stack is created) Note: The -stack linker option can be used to modify both of these values.The default stack size is taken from the process default reserve stack size. The operating system will grow the stack as needed by committing 1 page blocks (4K on an x86 machine) out of the reserved stack memory. Once all of the reserved memory has been committed, Windows NT will attempt to continue to grow the stack into the memory adjacent to the memory reserved for the stack, as shown in the following example on an x86 machine:
|<--- Total 1 MB for stack --->|<--- Adjacent memory --->| -------------------------------------------------------- | | | | | 4K | 1020K ... | ... | | | | | --------------------------------------------------------However, once the stack grows to the point that the adjacent area is not free (and this may happen as soon as the reserved 1 MB has been committed), the stack cannot grow any farther. Therefore, it is very risky to rely on this memory being free. Applications should take care to reserve all the memory that will be needed by increasing the amount of memory reserved for the stack. In other cases, it may be desirable to reduce the amount of memory reserved for the stack. The /STACK option in the linker and the STACKSIZE statement in the DEF file can be used to change both the amount of reserved memory and the amount of committed memory. The syntax for each method is shown below:
/STACK:[reserve][,commit] STACKSIZE [reserve][,commit] MORE INFORMATIONEach new thread gets its own stack space of committed and reserved memory. CreateThread() has a stacksize parameter, which is the commit size. If a new size is not specified in the CreateThread() call, the new thread takes on the same stack size as the thread that created it, whether that be the default value, a value defined in the DEF file, or by the linker switch. If the commit size specified is larger than the default process stack size, the stack size is set to the commit size. When specifying a stack size of 0, the commit size is taken from the process default commit. The system handles committing more reserved stack space when needed, but cannot reserve or commit more than the total amount initially reserved (or committed if no additional is reserved). Remember that the only resource consumed by reserving space is addresses in your process. No memory or pagefile space is allocated. When the memory is actually committed, both memory and pagefile resources are allocated. There is no harm in reserving a large area if it might be needed. As always, automatic variables are placed on the stack. All other static data is located in the process address space. Because they are static, they do not need to be managed like heap memory. Note that under Win32s 1.2 and earlier, stacks are limited to a maximum of 128K (this limit has been increased with Win32s 1.25a). The same stack is used on the 16-bit side of a Universal Thunk (UT). A 16:16 pointer is created and it points to the top of the 32-bit stack. The selector base is set in such a way that the 16-bit code is allocated at least an 8K stack.
|
KBCategory: kbprg
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |