How can I find current thread's maximum stack size?
I am getting a stack overflow exception while executing a function from MMC UI but not from Powershell (command-line/console). So I am kind of guessing its something to do with the default stack size allocated in UI thread to that of in Powershell (command line/console).
So how to find current thread's maximum stack size?
I know ideally one doesn't need to know these or set these, but looks like its related to stack size as it works from console/Powershell(command-line app) not from UI.
The below thread is kind of related, but it doesn't answer my question; it probably gives some guidelines:
Maximum Thread Stack Size .NET?
To get more details about the actual problem:
StackOverFlowException: Is it programming error (recursion) or not enough maximum default stack size?
You can call the getrlimit () function to get the current stack size. Typical maximum size of a stack is 1 MB on Widows, while on a typical modern Linux it is 8 MB, though those values can be adjusted in a number of ways.
Default stack size for 64bit processes is 4MB, it is 1MB for 32bit processes. You can modify the main-threads stack size by changing the value in its PE header. You can also specify the stack size by using the right overload of the Thread constructor.
The default stack reservation size used by the linker is 1 MB. To specify a different default stack reservation size for all threads and fibers, use the STACKSIZE statement in the module definition (. def) file.
The stack size estimation method is a technique for estimating the number of bytes of memory that will be needed to execute a given program on a particular machine. It uses timing data from the last three stages of the execution process: loading, running, and unloading.
From Windows 8, there is the GetCurrentThreadStackLimits() function. You can use it from C# via PInvoke like this:
[DllImport("kernel32.dll")]
static extern void GetCurrentThreadStackLimits(out uint lowLimit, out uint highLimit);
uint low;
uint high;
GetCurrentThreadStackLimits(out low, out high);
var size = (high - low) / 1024; // in KB
On my machine, this yields 1MB in a console application, 256KB in a web application (IIS).
Getting this information is a real PITA actually:
ThreadBasicInformation
as THREADINFOCLASS
to get a THREAD_BASIC_INFORMATION
structure. You now have the TebBaseAddress
parameter that is the address of the Thread Environment Block.TebBaseAddress
address.StackLimit
property which is the value you're looking for.From step 3, it's undocumented. That's why I do not recommend retrieving this information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With