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

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

问题描述

在下面的程序:

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 somewhere else? If its stored somewhere else, how are the memory locations connected?

推荐答案

内存静态变量通常在举行一些根植(或隐藏) [对象] 。此可以看出,在WinDbg中(与SOS)在物体上做一个!gcroot

Memory for static variables are normally held in some rooted (and hidden) object[]. This can be seen doing a !gcroot on the object in WinDbg (with SOS).

我想补充,这些引用永远不能GC'ed(除非你空场),我最近发现的。

Just to add, these references can never be GC'ed (unless you null the field), as I discovered recently.

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

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