Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find current thread's max stack size in .net?

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?

like image 673
Dreamer Avatar asked Jan 27 '12 03:01

Dreamer


People also ask

How do you calculate Max 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.

How big is the stack in C#?

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.

What is the default size of the stack?

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.

What is the stack size of a process?

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.


2 Answers

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).

like image 121
Michael Ganß Avatar answered Sep 21 '22 01:09

Michael Ganß


Getting this information is a real PITA actually:

  1. Get the thread ID using GetCurrentThreadId
  2. Use OpenThread to get a handle to the thread
  3. Now use NtQueryInformationThread to get information about the thread. You'll use 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.
  4. Read in process memory at the TebBaseAddress address.
  5. Within the Thread Environment Block (TEB), you have access to the 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.

like image 33
ken2k Avatar answered Sep 21 '22 01:09

ken2k