C#中的堆栈容量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/823724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stack capacity in C#
提问by
Could someone tell me what the stack capacity is in C#.
有人可以告诉我 C# 中的堆栈容量是多少。
I am trying to form a 3D mesh closed object using an array of 30,000 items.
我正在尝试使用 30,000 个项目的数组来形成 3D 网格闭合对象。
回答by Brian Rasmussen
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.
.NET 应用程序的默认堆栈大小为 1 MB(32 位 ASP.NET 应用程序的默认堆栈大小为 256 KB,64 位 ASP.NET 应用程序的默认堆栈大小为 512 KB),但您可以更改它。对于应用程序,您可以通过修改可执行文件的 PE 标头来更改默认大小。对于您创建的线程,您可以使用采用堆栈大小的构造函数重载。
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).
但正如 Anton Tyjhyy 在他的回答中指出的那样,数组是引用类型,因此位于堆上(即使数组恰好包含一堆值类型)。
回答by Dave Webb
The stack size is configurable and can be set in several different ways.
堆栈大小是可配置的,可以通过几种不同的方式进行设置。
回答by Anton Tykhyy
Your array will live on the heap, stack size is irrelevant in your case.
您的数组将存在于堆中,堆栈大小与您的情况无关。
回答by Anton Tykhyy
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.
要使用堆栈存储数组,您必须使用带有指针和 stackalloc 的不安全代码在堆栈上分配所需的内存空间。
回答by RBT
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:
如果要检查当前 .NET 程序集的值,则可以使用ILDASM
Visual Studio 命令提示符附带的命令来执行此操作。启动该工具后,打开您的程序集,然后转到View -> Headers
菜单。现在向下滚动到PE Optional Header (32 bit)
新打开的Headers
窗口中的部分。您将看到两个字段:
- Size of stack reserve - This is self-explanatory. This is default stack memory size allocated to any thread getting created in your program/application.
- Size of stack commit - committed stack space is - (Quoting Hans Passant from here)
- 堆栈保留的大小 - 这是不言自明的。这是分配给在您的程序/应用程序中创建的任何线程的默认堆栈内存大小。
- 堆栈提交的大小 - 提交的堆栈空间是 - (从这里引用 Hans Passant )
The said space is reserved in the operating system's paging file so the stack can always be swapped out when necessary.
所述空间保留在操作系统的分页文件中,因此在必要时始终可以换出堆栈。