5

What is the difference between reserve argument and commit argument to CreateThread Windows API function?

I can't understand the following lines ..

The reserve argument sets the amount of address space the system should reserve for the thread's stack. The default is 1 MB. The commit argument specifies the amount of physical storage that should be initially committed to the stack's reserved region.

these two lines you will find them in this paragraph which explains one of the parameters of the CreateThread function in c++

cbStackSize

The cbStackSize parameter specifies how much address space the thread can use for its own stack. Every thread owns its own stack. When CreateProcess starts a process, it internally calls CreateThread to initialize the process' primary thread. For the cbStackSize parameter, CreateProcess uses a value stored inside the executable file. You can control this value using the linker's /STACK switch:

/STACK:[ reserve][, commit]

The reserveargument sets the amount of address space the system should reserve for the thread's stack. The default is 1 MB. The commitargument specifies the amount of physical storage that should be initially committed to the stack's reserved region.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
Rehab Reda
  • 193
  • 7
  • 16
  • Can you please reformulate the question so it's immediately clear arguments to _which function_ are you talking about? ... or I'll try to do it for you. – Jan Hudec Jun 17 '14 at 09:50
  • it is mentioned above "these two lines you will find them in this paragraph which explains one of the parameters of the createthread function in c++" – Rehab Reda Jun 17 '14 at 09:54
  • Questions are editable here. I have edited the question to make it more obvious and applied proper formatting. You can edit it further if you don't like it. – Jan Hudec Jun 17 '14 at 09:56
  • By the way, there is no `createthread` function in Windows API, only `CreateThread`. C and C++ are case sensitive. – Jan Hudec Jun 17 '14 at 09:57

3 Answers3

7

The distinction is distinction between virtual and physical memory.

In any operating system worthy of that name, including Windows, pointers don't designate locations on the memory chip directly. They are locations in process-specific virtual memory space and the operating system then allocates parts of the physical memory chip to store the content of the parts where the process actually stores anything on demand. And may swap some data to disk when it runs out of RAM.

The reserve is the size of continuous virtual memory block to allocate for the stack. Below and above the range other things will be stored, so the reserve puts upper limit on how big the stack can grow.

Fortunately virtual memory is usually plentiful. You have 2GiB on 32-bit Windows, 3GiB if you link with /LARGEADDRESSAWARE flag and huge amount if you compile for 64-bits (x64). Only exception is WinCE before 5.0, where you only have 32MiB. So unless you are creating zillions of threads, you can be generous here and you should be, because if you don't have enough, the process will crash.

The commit is the size of physical memory that the system should preallocate for the stack. This makes the system immediately get some space in the physical memory, which is a shared resource and may be scarce. It may need to swap or discard it's previous content to get it. When you exceed it, the system will automatically scramble some more at the cost of small delay. So the only thing you gain by increasing the value here is a little speed-up if you actually have need for the memory. It's slow down if you don't. So you should be conservative here.

The stack is where local variables are placed. If you use large local buffers—and it is often reasonable since stack allocation is much faster than heap allocation (via malloc/new/anything that uses std::allocator)—you need to reserve enough stack. If you don't, the 1MiB is usually plenty.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
3

The reserve places a ceiling on how much stack space the thread will have. The commit places a floor on it. So it starts out consuming commit amount of memory and stops consuming when it hits reserve.

CDahn
  • 1,795
  • 12
  • 23
1

Every process has an address space. Stack of each thread is located somewhere in this space. When the tread is created, OS allocates piece of address space of the size reserve.

But it does not assign any real memory to all this space. It assigns only commit amount of memory.

The stack can grow over the time and OS will add more pages to it, extending the commit amount. But it cannot grow indefinitely. It cannot grow bigger than reserve.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51