最大线程堆栈大小.NET? [英] Maximum Thread Stack Size .NET?

查看:17
本文介绍了最大线程堆栈大小.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#.NET 2.0 中线程允许的最大堆栈大小是多少?此外,此值是否取决于 CLR 的版本和/或底层操作系统的位数(32 或 64)?我查看了以下资源 msdn1msdn2

What is the maximum stack size allowed for a thread in C#.NET 2.0? Also, does this value depend on the version of the CLR and/or the bitness (32 or 64) of the underlying OS? I have looked at the following resources msdn1 and msdn2

public Thread(
    ThreadStart start,
    int maxStackSize
)

我能看到的唯一信息是默认大小为 1 兆字节,在上述方法中,如果 maxStackSize 为0",则将使用在头文件中为可执行文件指定的默认最大堆栈大小,最大值是多少我们可以将标题中的值更改为?也建议这样做吗?谢谢.

The only information I can see is that the default size is 1 megabytes and in the above method, if maxStackSize is '0' the default maximum stack size specified in the header for the executable will be used, what's the maximum value that we can change the value in the header upto? Also is it advisable to do so? Thanks.

推荐答案

为了记录,这符合 Raymond Chen 的如果你需要知道,那么你做错了什么"的类别.

For the record, this fits Raymond Chen's category of "if you need to know then you are doing something wrong".

运行 64 位代码的线程的默认堆栈大小为 4 兆字节,32 位代码为 1 兆字节.虽然 Thread 构造函数允许您将整数值传递给 int.MaxValue,但您永远无法在 32 位机器上得到它.堆栈必须适合虚拟内存地址空间中的可用孔,通常在进程生命周期的早期达到约 600 MB.随着您分配内存和分割地址空间,它会迅速变小.

The default stack size for threads running 64-bit code is 4 megabytes, 1 megabyte for 32-bit code. While the Thread constructor lets you pass a integer value up to int.MaxValue, you'll never get that on a 32-bit machine. The stack must fit in an available hole in the virtual memory address space, that usually tops out at ~600 MB early in the process lifetime. Rapidly getting smaller as you allocate memory and fragment the address space.

分配超过默认值是完全没有必要的.当你有一个大量递归的方法会破坏堆栈时,你可能会考虑这样做.不要,修复算法,否则当工作变得更大时,无论如何你都会搞砸它.

Allocating more than the default is quite unnecessary. You might contemplate doing this when you have a heavily recursive method that blows the stack. Don't, fix the algorithm or you'll blow it anyway when the job gets bigger.

.NET 允许您选择的最小堆栈为 250 KB.如果您传递一个较小的值,它会默默地四舍五入.这是必要的,因为抖动和垃圾收集器都需要堆栈空间来完成它们的工作.同样,这样做应该是完全没有必要的.如果您考虑这样做是因为您有很多线程并且使用它们的堆栈消耗所有虚拟内存,那么您有太多线程.StackOverflowException 是您可以获得的最糟糕的运行时异常之一.进程死亡是直接且无法捕获的.

The smallest stack that .NET lets you choose is 250 KB. It silently rounds it up if you pass a value that's smaller. Necessary because both the jitter and the garbage collector need stack space to get their job done. Again, doing so should be quite unnecessary. If you contemplate doing so because you have a lot of threads and consume all virtual memory with their stacks then you have too many threads. A StackOverflowException is one of the nastiest runtime exceptions you can get. Process death is immediate and untrappable.

主线程的堆栈大小由 EXE 标头中的选项确定.编译器没有选项可以改变它,你必须使用 editbin.exe/stack 来修补 .exe 头文件.

The stack size for the main thread is determined by an option in the EXE header. The compiler doesn't have an option to change it, you have to use editbin.exe /stack to patch the .exe header.

这篇关于最大线程堆栈大小.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆