How to change stack size for a .NET program?

Frank picture Frank · Apr 1, 2010 · Viewed 22.3k times · Source

I have a program that does recursive calls for 2 billion times and the stack overflow. I make changes, and then it still need 40K recursive calls. So I need probably several MB stack memory. I heard the stack size is default to 1MB. I tried search online. Some one said to go properties ->linker .........in visual studio, but I cannot find it.

Does anybody knows how to increase it? Also I am wondering if I can set it somewhere in my C# program?

P.S. I am using 32-bit winXP and 64bit win7.

Answer

Andrew O'Reilly picture Andrew O'Reilly · Apr 1, 2010

The easiest way to set the stack size from .NET 2.0 and Win XP onwards is to spawn a new thread with the stack size you'd like:-

using System.Threading;

Thread T = new Thread(threadDelegate, stackSizeInBytes);
T.Start();

To change the stack size of the entire program you'd have to use editbin:-

EDITBIN.EXE /STACK:<stacksize> file.exe