暗战值类型的堆栈空间 [英] Running out of ValueType stack space

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

问题描述

我的理解是,在堆栈空间的.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...

所以我的问题是这样的;这是否意味着行为的任何值类型变量声明是有限的存储空间1MB?你已经在你当前范围内声明的更多的值类型调用堆栈可以有效的是更小,在某些时候,这是否意味着申报(为参数的缘故)〜26万整数会使用所有的栈空间?

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.

让我们本次测试我们看到什么是可能的,什么是不。我是一个64位机VS2010和C#4.0编译器上。

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.

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

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