用完 ValueType 堆栈空间 [英] Running out of ValueType stack space

查看:15
本文介绍了用完 ValueType 堆栈空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是 .Net 中的每个新线程都会分配 1MB 的堆栈空间.进一步我的理解是值类型存储在堆栈而不是堆中......

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...

所以我的问题是这样的;这种行为是否意味着任何 ValueType 变量声明都被限制为 1MB 的存储空间?您在当前范围内声明的 ValueType 越多,调用堆栈可以有效地越小,这是否意味着声明(为了论证)~260,000 个整数将使用您的所有堆栈空间?

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?

推荐答案

这类问题最有成效的答案通常是去测试.其他人告诉你,你不应该担心这个,他们是对的.所有链接的文章都很棒,值得一读.在大多数实际情况下,您不需要接近 1MB 的局部变量.

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.

但是,如果您想知道实际可以拥有 1MB 局部变量价值的天气怎么办?正如其他人指出的那样,这是实现细节,结果可能因平台、编译器版本、供应商等而异.

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.

让我们自己测试一下,看看什么是可能的,什么是不可能的.我在带有 VS2010 和 C# 4.0 编译器的 x64 机器上.

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.

这是我的代码:

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.

再说一次:这些考虑几乎是不切实际的.如果你分配了 1MB 的局部变量,你很可能做错了什么.但万一你想知道......现在你知道了.

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.

这篇关于用完 ValueType 堆栈空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆