[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference decwet::nt-developers

Title:MS Windows NT Developers
Notice:See note 1222 for MS bug reporting info
Moderator:TARKIN::LINEIBER
Created:Mon Nov 11 1991
Last Modified:Tue Jun 03 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3247
Total number of notes:15633

3208.0. "threads / stack size / VM size" by EDSCLU::JAYAKUMAR () Fri Mar 14 1997 11:57

Folks,

I need some help in tuning my application.

1. Whats the default stack size for a thread in NT?  The doc. is not very clear
   (see for the extract of the doc at the end).

         A sample test program which creates 500 threads (with no local/global 
         variables) had 12248K as the VM size. So is the default stack size 
         for a thread ~ 24K?

2. My next, more important question is how do I reduce this stack size, so that 
   I can reduce the memory size (VM) of my application which creates a large
   number of threads? 

         These following options:

             - linker /STACK,
             - dwStack parameter in CreateThread(),
             - using Editbin

         supposidely exists to change the default stack size. But unfortunately 
         they only help me in increasing it but not reducing it.

Thanks
-Jay
============================================================================

		Under linker options:

>>>>
Stack Allocations

This option sets the size of the stack in bytes. Command-line equivalent: 
/STACK:reserve[,commit]

The Reserve text box (or the reserve argument on the command line) specifies 
the total stack allocation in virtual memory. The default stack size is 1 MB. 
>>>>


		Under CreateThread:

>>>>
dwStackSize

Specifies the size, in bytes, of the stack for the new thread. If 0 is
specified, the stack size defaults to the same size as that of the primary
thread of the process. The stack is allocated automatically in the memory space
of the process and it is freed when the thread terminates. Note that the stack
size grows, if necessary.
>>>>>                               


Thanks
-Jay

T.RTitleUserPersonal
Name
DateLines
3208.1EDSCLU::JAYAKUMARTue Mar 18 1997 11:268
Any hints, tips on how I might lower the thread stack size than the default
value (at thread creation time) so that I can decrease the mem size of the 
application).

I was browsing the Knowledge Base CD, and there isn't much more than what the 
SDK doc. already has. 

-Jay
3208.2POLAR::MOKHTARTue Mar 25 1997 12:4634
    
    First you should distinguish between reserved memory and commited
    memory. 
    Reserved memory reserves addresses in virtual memory space. committed 
    memory commits memory already reserved from virtual space to physical 
    memory and/or page file. 
    
    The default reserved stack space for each thread is 1M virtual space
    ( under this value you are limited to at most 2000 threads ). However 
    the default commited memory from this reserved space is 4K. If a thread
    actually needs to reference space beyond this 4K page, an internal
    exception is generated and NT will trap it and commit more memory, in
    4K chuncks, from the 1M virtual space into physical memory/page file.
     
    This method allows continious address space to be reserved/available 
    for the stack without requiring physical memory/page file unless it is 
    really needed, and when it is needed it is allocated/commited in 4K 
    chuncks.
    
    The 1M default value can be changed through the linker STACK option.
    The 4K default value for commit chunk can be changed by specifying a 
    specific value in the beginthread/createthread 'stack size' parameter.
    
    On a side note : one can also take advantage of reserve versus commit 
    memory by allocating memory using the VirtualAlloc group of functions.
    Howerver if memory is allocated via GlobalAlloc/LocalAlloc or C run
    time library malloc, it will be reserved and commited in one step. My
    recollection is the heap assigned for them is only 1M ( default heap
    for the entire process ). If you wish a bigger heap you can create a 
    new heap and use HeapAlloc, which also reserves and commits in 1 step.
    
    hope this helps maged 
    
    
3208.3BHAJEE::JAERVINENOra, the Old Rural AmateurTue Mar 25 1997 14:227
    This was already discussed in (I think) the Visual C++ conference (I'd
    wish coss-postings were indicated).
    
    Anyway, it turns out that the autor of .0 is running on an Alpha, i.e.
    page size 8k. Thus the minimum (virtual) stack space is 24k (one page,
    and a guard page at both sides).
    
3208.4EDSCLU::JAYAKUMARWed Mar 26 1997 15:168
re: .2

Thanks for your explanation. It appears that the stack size cannot be less 
than 3 pages, which happens to be 24K on Alpha.

Note 1096 HUMANE::VISUALC has some discussion on the same issue.

-Jay