58

Could someone tell me what the stack capacity is in C#.

I am trying to form a 3D mesh closed object using an array of 30,000 items.

Community
  • 1
  • 1
  • 5
    You could just test it yourself by putting trillions of items on it and see when it crashes? – Smashery May 05 '09 at 07:21
  • A closely related question is [Stack size under Mono](http://stackoverflow.com/questions/19817790/stack-size-under-mono). – Palec Nov 14 '13 at 20:46

4 Answers4

75

The default stack size for a .NET application is 1 MB (default is 256 KB for 32-bit ASP.NET apps and 512 KB for 64-bit ASP.NET apps), but you can change that. For the application you can change the default size by modifying the PE header of the executable. For threads you create, you can use the constructor overload that takes a stack size.

But as Anton Tyjhyy points out in his answer, arrays are reference types and thus located on the heap (even if the array happens to hold a bunch of value types).

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 3
    The 256 KB size for ASP.Net applications is for 32-bit applications. On 64-bit Windows Server 2008 and higher, the stack size has been pushed up to 512 KB. See KB 932909 for further details. – Dono Oct 28 '13 at 03:05
  • 2
    @BrianRasmussen You can actually allocate an array on the stack using `stackalloc`. Additionally, just because something is a reference type does not necessarily mean it will be allocated on the stack or the heap, it varies by implementation of the CLR and in the .NET implementation it varies in a few special cases (size of the type and scope). – Michael J. Gray Oct 28 '13 at 21:56
  • @MichaelJ.Gray I'm not aware of any implementations where instances of reference types don't go on the managed heap. Do you have an example of that? – Brian Rasmussen Oct 28 '13 at 22:31
  • @BrianRasmussen Check out Eric Lippert's article [The Stack Is An Implementation Detail](http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx). It's a thought experiment. And a good read, too. – Cristian Diaconescu Nov 27 '13 at 13:19
  • @CristiDiaconescu Thanks. I know the article. – Brian Rasmussen Nov 27 '13 at 16:31
  • Thanks @BrianRasmussen for sharing the information about PE header. Could you please share doc for updating the default size in the PE header of the executable ? – Kumar Jul 13 '22 at 16:07
55

Your array will live on the heap, stack size is irrelevant in your case.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
7

If you want to check the value for your current .NET assembly then you can do so by using ILDASM command that comes in with Visual Studio command prompt. Once you have started the tool, open your assembly and then go to View -> Headers menu. Now scroll down to PE Optional Header (32 bit) section in the newly opened Headers window. You will see two fields:

  1. Size of stack reserve - This is self-explanatory. This is default stack memory size allocated to any thread getting created in your program/application.
  2. Size of stack commit - committed stack space is - (Quoting Hans Passant from here)

The said space is reserved in the operating system's paging file so the stack can always be swapped out when necessary.

enter image description here

RBT
  • 24,161
  • 21
  • 159
  • 240
5

To use stack for storing an array you have to use unsafe code with pointers and stackalloc to allocate desired memory space on the stack.