Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change stack size for a .NET program?

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.

like image 441
Frank Avatar asked Mar 31 '10 22:03

Frank


People also ask

Can we change the size of stack?

You can do this by using the "ulimit -s" command in linux which will set the stack size for all process executed under that shell. Incase of windows, the same can be done in VC6 for that project by setting the stack size in Project Properties->link options->output->stack allocations->reserve.

How do you specify stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

What is stack size in C#?

Today's PCs have a large amount of physical RAM but still, the stack size of C# is only 1 MB for 32-bit processes and 4 MB for 64-bit processes (Stack capacity in C#).

How big is the .NET stack?

In Microsoft Windows 2000, if the Microsoft ASP.NET Worker Process (ASPNet_wp.exe) creates a thread, the maximum stack size of the thread is 1 MB. In Windows Server 2008 and higher, the maximum stack size of a thread running on 32-bit version of IIS is 256 KB, and on an x64 server is 512 KB.

How do I increase the stack size of a program?

The space between /F and number is optional. You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

How do I set the stack size in Visual Studio?

You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations). Using EDITBIN on the EXE file. For more information, see EDITBIN reference. Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio.

How do I set the stack size in the linker?

The linker rounds up the specified value to the nearest 4 bytes. The space between /F and number is optional. You may need to increase the stack size if your program gets stack-overflow messages. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK.

What is the default stack size in C++?

The stack size in bytes. Without this option, the stack size defaults to 1 MB. The number argument can be in decimal or C-language notation. The argument can range from 1 to the maximum stack size accepted by the linker. The linker rounds up the specified value to the nearest multiple of 4 bytes.


2 Answers

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 
like image 106
Andrew O'Reilly Avatar answered Oct 07 '22 20:10

Andrew O'Reilly


There is no compiler option to do it. You can edit it after the fact using editbin /stack, or create a separate thread for your algorithm, and specify a larger stack size in the Thread constructor.

That being said, you may want to flatten your recursive function... If you're having stack overflows now, it's tough to know that any stack size will be appropriate in the long term. This is just a band-aid solution.

like image 43
Reed Copsey Avatar answered Oct 07 '22 20:10

Reed Copsey