简体   繁体   English

垃圾收集器如何处理静态类使用的类型对象?

[英]How does the garbage collector handle Type Objects used by static classes?

Suppose I have the following class: 假设我有以下课程:

 public class Class1
 {
      private Class2 _class2;

      public void SomeMethod() 
      { 
           _class2 = new Class2();               
      }
 }

This is my understanding of what happens when I call var instance = new Class1() : 这就是我对调用var instance = new Class1()时发生的情况的理解:

  • If it does not already exist, a new Type Object is created on the heap for Class1 如果尚不存在,则在堆上为Class1创建一个新的Type Object
  • instance is created on the heap, with it's Type Object Pointer pointing to the Class1 Type Object instance是在堆上创建的,它的类型对象指针指向Class1类型对象

Question 1 : Will a Type Object be created for Class2 because Class1 references it, even though I have not instantiated one yet (assuming it does not already exist of course)? 问题1 :是否将为Class2创建类型对象,因为Class1引用了它,即使我还没有实例化它(假设它当然还不存在)?

Now suppose I have the following static class 现在假设我有以下静态类

 public static class StaticClass
 {
      private static NormalClass _normalClass = new NormalClass();
      public static void SomeMethod() 
      { 
           // Does something using _normalClass 
      }
 }

When I call SomeMethod() , the StaticClass Type Object is created on the heap. 当我调用SomeMethod() ,将在堆上创建StaticClass类型对象。

_normalClass is also created and so is the NormalClass Type Object. _normalClass也被创建, NormalClass类型对象也被创建。

I know that the StaticClass Type Object will never be garbage collected. 我知道StaticClass类型对象将永远不会被垃圾收集。 Any because it holds a reference to _normalClass , neither will that. 因为它拥有对_normalClass的引用,所以不会。

Question 2 : Are Type Objects garbage collected? 问题2 :是否收集了类型对象垃圾? If so, will the Type Object for NormalClass ever be eligable for garbage collection? 如果是这样, NormalClass的类型对象是否有资格进行垃圾回收? What about the Type Objects for anything referenced inside NormalClass ? NormalClass内部引用的任何类型对象呢?

When I create an innocent looking static class, am I potentially filling up the heap with a large 'chain' of Type Objects that can never be garbage collected? 当我创建一个看起来很纯真的静态类时,是否有可能用无法被垃圾回收的大型“类型对象”链填充堆?

Okay, your idea of how the CLR works is incorrect. 好的,您对CLR如何工作的想法是不正确的。 Please refer to this article on the .NET Internals if you'd like to fully understand how it works: 如果您想完全了解它是如何工作的,请参考.NET内部的这篇文章:

http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S7 http://msdn.microsoft.com/zh-CN/magazine/cc163791.aspx#S7

First, lets get your understanding correct: 首先,让您正确理解:

  1. Unless you're using reflection, there are no type objects that are created on heap. 除非使用反射,否则不会在堆上创建任何类型对象。
  2. When an object is allocated, its has a "TypeHandle", which points to the information about the type. 分配对象时,其具有“ TypeHandle”,它指向有关类型的信息。

Now, to your questions: 现在,对您的问题:

Answer No. 1: 答案一:

No. Memory allocations happen for Class1 on the heap when its instantiated. 不会。实例化堆时会在堆上为Class1进行内存分配。 Class2, being a reference type, gets a reference pointer. 作为引用类型的Class2获取引用指针。 So, memory to hold the reference pointer is allocated. 因此,分配了用于保存参考指针的内存。 Period. 期。

When, Class2 is instantiated, memory for the actual storage (based on field sizes) is allocated in the heap. 实例化Class2时,将在堆中分配用于实际存储的内存(基于字段大小)。

Answer No. 2: 答案二:

Yes, but they are not what you assumed they are. 是的,但是它们不是您认为的那样。

Could the _normalClass ever be garbage collected? _normalClass是否曾经被垃圾回收? Yes. 是。 If its set to null at some point, it will be collected when the GC runs. 如果在某些时候将其设置为null,则它将在GC运行时收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM