Stack capacity in C#

2019-01-03 10:18发布

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.

标签: c# .net clr stack
5条回答
来,给爷笑一个
2楼-- · 2019-01-03 10:42

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

查看更多
放我归山
3楼-- · 2019-01-03 10:45

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.

查看更多
三岁会撩人
4楼-- · 2019-01-03 10:46

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-03 10:49

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

查看更多
Evening l夕情丶
6楼-- · 2019-01-03 11:07

The stack size is configurable and can be set in several different ways.

查看更多
登录 后发表回答