Maxim Gershkovich
Maxim Gershkovich

Reputation: 47179

In the context of the CLR in .NET how is stack space allocated

In the context of the CLR in .NET how is stack space allocated and what is it typically limited by?

For example:

Can any given thread continue to add to the stack until memory runs out? If not; how does the CLR decide how much space to allocate and can it change its mind?

PS: Just to put some context on it this all started from a discussion on how to build a method that will calculate the Fibonacci sequence and one of the suggestions was a recursive function.

Upvotes: 2

Views: 570

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

The CLR immediately commits the full stack space for each thread, as soon as it is created. The default stack size is 1MB. If you push your stack over that size, that's a stack overflow and an error is raised.

The CLR adopts a different policy from native code which merely reserves 1MB but commits it on demand.

Those are implementation details. It is best to simply view the stack as a fixed size data structure. This view fits with both .net and native stack implementations.

A recursive function is the absolute worst way to calculate Fibonacci because its complexity is exponential. Instead you should use an iterative algorithm which is linear in time and constant in space. For example:

static int fib(int n)
{
    int result = 0;
    int a = 1;
    for (int i=1; i<=n; i++)
    {
        int temp = result;
        result = a;
        a = temp + a;
    }
    return result;
}

Of course, in reality, you'd overflow your result variable long before you reached a stack overflow. But the performance of a recursive algorithm is untenable for values of n in the region of 30-40.

Yet another sensible approach is to fill a static array with pre-calculated values. Again since the values grow so fast you don't need a very large array for it to contain all values that fit into an int or even a long.

Upvotes: 4

Related Questions