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

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

问题描述

C#.NET 2.0中线程允许的最大堆栈大小是多少?另外,此值是否取决于CLR的版本和/或基础OS的位数(32或64)? 我查看了以下资源 msdn1 msdn2

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 )

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天全站免登陆