问题
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. WhenCreateProcess
starts a process, it internally callsCreateThread
to initialize the process' primary thread. For thecbStackSize
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.
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/24260638/what-is-the-difference-between-reserve-and-commit-argument-to-createthread