Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:44:33+00:00 2026-05-22T00:44:33+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:44:33+00:00Added an answer on May 22, 2026 at 12:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder whether .NET CLR supports P/Invoke coarsening. For example if we have three
Context: Windows7 64bit, ActiveDirectory, Windows Server 2003 I'm trying to get the code given
Context: I have to add an online payment system, on a quite old PHP
Context: I'm in charge of running a service written in .NET. Proprietary application. It
Im studying up on .NET multifile assemblies. In the context of multifile assemblies, the
Thanks in advance for any assistance anyone can provide with this torturous issue. I
using the following LINQ query: EntityQuery<Questions> query = context.GetQuestionsQuery() .Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID
Context : an application uses a piece of Rack middleware that must be setup
Context Say, we want to use the Box-Muller algorithm. Starting from a couple of
Context: So, I am attempting to build a ridiculously complex domain model. Talking with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.