Maximum Thread Stack Size .NET?

11,138

Solution 1

For the record, this fits Raymond Chen's category of "if you need to know then you are doing something wrong".

The default stack size for threads running 64-bit code is 4 megabytes, 1 megabyte for 32-bit code. While the Thread constructor lets you pass a integer value up to int.MaxValue, you'll never get that on a 32-bit machine. The stack must fit in an available hole in the virtual memory address space, that usually tops out at ~600 MB early in the process lifetime. Rapidly getting smaller as you allocate memory and fragment the address space.

Allocating more than the default is quite unnecessary. You might contemplate doing this when you have a heavily recursive method that blows the stack. Don't, fix the algorithm or you'll blow it anyway when the job gets bigger.

The smallest stack that .NET lets you choose is 250 KB. It silently rounds it up if you pass a value that's smaller. Necessary because both the jitter and the garbage collector need stack space to get their job done. Again, doing so should be quite unnecessary. If you contemplate doing so because you have a lot of threads and consume all virtual memory with their stacks then you have too many threads. A StackOverflowException is one of the nastiest runtime exceptions you can get. Process death is immediate and untrappable.

The stack size for the main thread is determined by an option in the EXE header. The compiler doesn't have an option to change it, you have to use editbin.exe /stack to patch the .exe header.

Solution 2

I am unaware of what the maximum is, but MSDN speaks to whether you should do it or not:

Avoid using this constructor overload. The default stack size used by the Thread(ThreadStart) constructor overload is the recommended stack size for threads. If a thread has memory problems, the most likely cause is programming error, such as infinite recursion.

I have never had a StackOverflow occur in C# which was not due to infinite recursion. If there truly was a case where recursion went to that depth, I would consider replacing it with iteration.

Share:
11,138

Related videos on Youtube

Oved
Author by

Oved

Updated on August 16, 2021

Comments

  • Oved
    Oved over 2 years

    What is the maximum stack size allowed for a thread in C#.NET 2.0? Also, does this value depend on the version of the CLR and/or the bitness (32 or 64) of the underlying OS? I have looked at the following resources msdn1 and msdn2

    public Thread(
        ThreadStart start,
        int maxStackSize
    )
    

    The only information I can see is that the default size is 1 megabytes and in the above method, if maxStackSize is '0' the default maximum stack size specified in the header for the executable will be used, what's the maximum value that we can change the value in the header upto? Also is it advisable to do so? Thanks.

  • Oved
    Oved about 13 years
    Thanks Chris. I have a background thread that does long operation on a set of files and fails with 'Insufficient memory to continue the execution of the program' error. This issue was once fixed by changing the maxStackSize value. Now for different set of files it fails with the same error and I was trying to set the value to the maximum possible and see the result .. Also just curious about the max value as I was not able to find it.