Is it possible to have too many methods in terms of stack size and possible overflow?

We all know that it’s good practice to create small methods that encourage reuse, which will inevitably lead to many methods that will be put on the stack. However, is it possible to reach a scenario where so many nested method calls raise a StackOverflow Exception?

Will the decision just increase the size of the stack?

The documentation states that such an exception will occur during "very deep or unlimited recursion", so this certainly seems possible, or does the .NET Framework dynamically handle the stack size for us?

My question can be summarized as follows:

Is it possible to have such a well-designed program (in terms of small reusable methods) that it becomes inappropriate to increase the size of the stack and, therefore, use more resources?

+5
source share
3 answers

Not really. I just did a very quick test, and a StackOverflowException throws after 15,000 nested calls.

You cannot write code that will not be embedded recursively 15,000 times due to the large number of methods that you have.

Obviously, the exact number depends on the many locally variables that you allocated on the stack. But whatever this actual number is, it is not so close as to do what you offer.

+2
source

.NET 1 .

​​ ( ), , , ?

.

, , - . (), , , (, Stack<T>).

+3

. - ( ), . GC, .

That way, I could create a program that is faster by allocating many things on the stack. Even using stackalloc (which is a lesser known C # / CLR function).

There are valid cases for this. They are rare. Just saying “no valid uses” is simply wrong.

0
source

All Articles