静态类/成员在哪里分配?
我已经很长时间没有试图找出静态类的真相了。我的观点是:使用 new 运算符时,值类型在堆栈中分配,引用类型在堆中分配。但静态类的本质是您无法创建它的实例,并且确保它不是值类型。所以我有一个问题,CLR 何时何地为静态内容分配内存?关于何时...我假设在编译期间,当构建程序集时,但我不确定。关于在哪里...当我试图找出我读 J.Richter 的“CLR via C#”时,他写道,当您创建该特定实例的实例时,实例类的静态方法会在堆中分配类(连同类型对象指针和同步块索引)。但我不明白。它是静态的。它不应该依赖于对象的任何状态。所以请减轻我的负担。
it's being a long time since i'm trying to find out the truth about static classes. my point is: value types are allocated in stack, reference types in heap, when using the new operator. but a nature of a static class is that you can't make an instance of it, and sure it's not a value type. so i have a question when and where does the CLR allocates a memory for static content? about when...i'm supposing during compilation, when an assembly is built, but i'm not sure. and about where...while i was trying to find out i read J.Richter's "CLR via C#", and he wrote, that static method of an instance-class is allocated in heap when you're creating an instance of that specific class(together with type object pointer and sync block index). but i don't get it. it's static. it shouldn't depend on any state of object. so please, lighten me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLR 维护多个与 AppDomain 关联的堆,统称为“加载器堆”。它们与垃圾收集堆不同,因为它们不包含可收集对象,主要是类型相关的数据。 AppDomain 生命周期内存在的数据类型。
静态变量的空间分配在其中之一,高频堆中。 JIT 编译器进行分配,它生成的代码直接引用内存位置。背景信息位于这篇MSDN 杂志文章中。
The CLR maintains several heaps associated with an AppDomain, collectively called "loader heaps". They are distinct from the garbage collected heap since they don't contain collectable objects, mostly type related data. The kind of data that's around for the lifetime of an AppDomain.
Space for static variables is allocated in one of them, the HighFrequencyHeap. The JIT compiler makes the allocation, the code it generates directly references the memory location. Background info is in this MSDN Magazine article.
除了用户堆内存之外,CLR 还保存各种簿记信息和信息。堆上的元数据。这包括您执行的方法的实际代码,以及加载到 AppDomain 中的每种类型的 Type 对象 - 它的名称、重载、无论是抽象的还是密封的,以及定义的所有方法(静态和实例)的列表类型。
当您执行方法时,CLR 在堆的仅 CLR 部分上的相应 Type 对象中查找方法信息并执行该方法。实例方法和静态方法之间的唯一区别是实例方法在方法参数中包含一个额外的“this”指针,指向该方法正在执行的实例。
因此,静态信息与其他所有信息一起存储在堆上,但它不与任何特定的对象实例关联。
As well as user heap memory, the CLR keeps various bookkeeping information & metadata on the heap. This includes the actual code for methods you execute, as well as a Type object for every type loaded into the AppDomain - it's name, overloads, whether it's abstract or sealed, and a list of all the methods (both static and instance) defined on the type.
When you execute a method, the CLR looks up the method information in the corresponding Type object on the CLR-only part of the heap and executes that method. The only difference between instance and static methods is that instance methods include an extra 'this' pointer in the method arguments pointing to the instance the method is executing on.
So, the static information is stored on the heap along with everything else, but it is not associated with any particular object instance.