如何内存分配给一个静态变量? [英] How's memory allocated for a static variable?

查看:238
本文介绍了如何内存分配给一个静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序

class Main{   
    static string staticVariable = "Static Variable";
    string instanceVariable = "Instance Variable";

    public Main(){}   
}

instanceVariable 将存储分配给对象实例的内存中。其中将 staticVariable 存储,是存储在对象实例本身还是有些还有什么地方?如果它存储了一些别的地方,如何存储位置连接?

The instanceVariable will be stored inside the memory allocated for object instance. Where will the staticVariable be stored, is it stored in the object instance itself or some where else? If its stored some where else, how are the memory locations connected?

推荐答案

静态变量保存在堆上,不管它是在引用类型和值类型声明。世界上只有一个总插槽不管有多少实例被创建。

Static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created.

这堆独立于常规的垃圾回收堆 - 它被称为高频堆,这里面的每个应用程序域的一个

This heap is separate from the normal garbage collected heap - it's known as a "high frequency heap", and there's one per application domain.

您会发现下面的资源非常有用 静态变量揭秘

You will find the below resource useful Static variable demystified

这篇关于如何内存分配给一个静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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