我们都知道,创建促进重用的小方法是一种很好的做法,这将不可避免地导致大量方法被放置在堆栈上。然而,是否有可能达到这样一种情况,即有如此多的嵌套方法调用 StackOverflow出现异常?
可以接受的解决方案是简单地增加堆栈大小吗?
documentation states在“非常深或无界的递归”期间会发生这样的异常,所以它看起来当然是可能的,或者 .NET 框架是否为我们动态处理堆栈大小?
我的问题可以这样总结:
Is it possible to have such a well designed program (in terms of small reusable methods) that is becomes necassary to increase the stack size and hence use more resources?
最佳答案
.NET 堆栈大小是固定的,默认为 1 MB。
Is it possible to have such a well designed program (in terms of small reusable methods) that is becomes necessary to increase the stack size and hence use more resources?
它不会在把你的逻辑分解成方法。
遇到不是直接错误的 Stack Overflow 的唯一方法是使用递归。当这种情况发生(威胁)时,不要增加堆栈,而是重写代码以使用不同的方式来存储数据(如 Stack<T>
)。
关于c# - 是否有可能在堆栈大小和可能溢出方面有太多方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575710/