静态成员存储在内存中的什么位置? C#.net中的堆栈/堆 [英] Where are static members stored in memory? stack/ heap in C# .Net

查看:118
本文介绍了静态成员存储在内存中的什么位置? C#.net中的堆栈/堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

较早的文章处理了值和引用类型及其内存分配。

The earlier post dealt with the value and reference types and their memory allocation.

在这里,我试图了解静态成员的内存分配。

Here I'm trying to understand the memory allocation of static members.

我有一个简单的类,它同时具有静态和非静态整数,如下所示。

I have a simple class which has got both static and non-static integers like one shown below.

 class Sample
 {
   public int nonStaticInt = 0;
   public  static int staticInt = 0;
 }

我的问题是,静态整数驻留在哪里?堆/一堆。以及在创建任何对象之前它们如何先进入内存。

My question here is, where do static integer reside? Stack/ a Heap. And how do they get into memory first even before any object creation.

谢谢!

推荐答案

分配静态变量后,它将将存储为Methodtable的一部分。
Methodtable的意思是,当第一次在应用程序中加载类时,将在appdomain中为类内的类级变量和方法分配单独的内存。

When a static variable is allocated, it will be stored as part of Methodtable. Methodtable means When a class is loaded first time in application, separate memory will be allocated in appdomain for class level variables and methods inside class. .

如果静态变量是原始类型,则将其存储为Methodtable的一部分。如果是引用类型,则将其存储在堆中,并将引用存储在Methodtable中。

If static variable is primitive type, it will be stored as part of Methodtable. If it is reference type, it will be stored inside the heap and the reference will be stored in Methodtable

这篇关于静态成员存储在内存中的什么位置? C#.net中的堆栈/堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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