The Windows Internal book 5th edition has the following comment in page 360.
The stack size for the initial thread is taken from the image—there’s no way to specify another size.
I understand that for Windows OS, each thread is given 4K or 16K (depending on system) stack, and the size is fixed.
Then how about the stack in .NET?
- How big is the stack?
- The size of the stack is fixed or variable?
- Is the stack allocated for each thread just like the case of Windows?
Yes, the size for the startup thread is determined by a value in the .EXE file header. Necessarily so, it is the OS that creates the thread, before any code in the program can run. It calls the entrypoint of the program, CorExeMain().
The managed compiler you use writes that value into the EXE file header. Current .NET compilers select 1 MB when you target x86 or Any CPU, 4 MB when you target x64. This is however not fixed, you can modify the value with the Editbin.exe utility, /STACK command line option. You could use this post-build event to get a 2MB stack:
The stack size for threads that you create yourself are under your control, the Thread class constructor has overloads that lets you specify the size. You cannot make it too small, if clips the value to 256 KB. That’s necessary, the just-in-time compiler also uses the stack.