Do value types get Garbage collected?

后端 未结 3 911
青春惊慌失措
青春惊慌失措 2020-12-08 02:55

I know that reference type will be garbage collected. I wanted to know whether value types will also be garbage collected from the stack?

3条回答
  •  时光取名叫无心
    2020-12-08 03:19

    The common language runtime (CLR) allocates memory for objects in two places: the stack and the heap.

    Value types are stored on the stack along with references to reference type content stored on the heap. The point of the garbage collector is to deallocate memory assigned to the reference type content on the heap when the reference is popped from the stack.

    A value type that is not the content of a reference type is not stored on the heap so it is not cleaned up by the garbage collector.

    See here for a slightly more in depth description of value vs reference types

    See here for a lot more in depth description

提交回复
热议问题