4

My understanding is that each new thread in .Net allocates 1MB of stack space. Further my understanding is that value types are stored on the stack rather then the heap...

So my question is this; does this behavior mean that any ValueType variable declarations are limited to 1MB of storage space? That the more ValueTypes you have declared in your current scope the smaller the callstack can effectively be and at some point does this mean that declaring (for the sake of argument) ~260,000 ints would use all your stack space?

8

1 Answer 1

4

A most fruitful answer to this type of question is usually go and test. Others told you that you should not worry about this and they are kind of right. All the articles that are linked are great and well worth reading. In most practical cases, you won't need anywhere near of 1MB of local variables.

But what if you want to know weather you actually can have 1MB of local variables worth? As others pointed out this is implementation details, and the results may vary depending on platform, compiler version, vendor, etc.

Let's test this ourselves and see what is possible and what is not. I'm on an x64 machine with VS2010 and C# 4.0 compiler.

Here is my code:

using System;

namespace SO6301703
{
    struct s64b
    {
        public long f1;
        public long f2;
        public long f3;
        public long f4;
        public long f5;
        public long f6;
        public long f7;
        public long f8;
    }

    struct s256b
    {
        public s64b f1;
        public s64b f2;
        public s64b f3;
        public s64b f4;
    }

    struct s1kb
    {
        public s256b f1;
        public s256b f2;
        public s256b f3;
        public s256b f4;
    }

    struct s8kb
    {
        public s1kb f1;
        public s1kb f2;
        public s1kb f3;
        public s1kb f4;
        public s1kb f5;
        public s1kb f6;
        public s1kb f7;
        public s1kb f8;
    }

    struct s64kb
    {
        public s8kb f1;
        public s8kb f2;
        public s8kb f3;
        public s8kb f4;
        public s8kb f5;
        public s8kb f6;
        public s8kb f7;
        public s8kb f8;

    }

    struct s512kb
    {
        public s64kb f1;
        public s64kb f2;
        public s64kb f3;
        public s64kb f4;
        public s64kb f5;
        public s64kb f6;
        public s64kb f7;
        public s64kb f8;

    }

    struct s1Mb
    {
        public s512kb f1;
        public s512kb f2;

    }

    class Program
    {
        static void Main(string[] args)
        {
            unsafe { Console.WriteLine(sizeof(s1Mb)); }
            s1Mb test;
        }
    }
}

When I compile and run this code, I am getting the stack overflow exception. This means that at least in some cases you are indeed limited by the stack space. And it does mean that the more local variables you have, the less stack you have left for method calls, recursion, etc.

Once again: these consideration are hardly ever practical. If you are allocating 1MB worth of local variables, you most likely doing something wrong. But in case you are wondering anyway... now you know.

3
  • Awesome! I guess the reason I asked the question (because I was almost certain my assumption was correct) was because I couldn't think of a practical way of testing the hypothesis. Jun 10, 2011 at 5:29
  • @Maxim Gershkovich: please consider accepting the answer if it satisfies you. Jun 10, 2011 at 7:31
  • Would not have expected this to fail. Good to know though. I got an error in Visual Studio when writing the code. Jun 15, 2011 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.