memory allocation of value types and reference types in .net framework

后端 未结 5 1991
独厮守ぢ
独厮守ぢ 2020-11-29 10:05

Is there an advanced article which I can read that can explain how memory is allocated for different types (value and reference) in .net framework.

for example we kn

5条回答
  •  天涯浪人
    2020-11-29 10:37

    When a method is called the amount of space required by value types is known in advance (it can be computed by the compiler). This space is allocated on the stack and is only available for the duration of the method call. For each new method call the memory used on the stack grows and when the method exits it shrinks back to it's previous level.

    Reference types are allocated on the heap. The heap is basically a block of memory used for that purpose. An object stored on the heap is primarily the fields of the object stored in the the memory allocated to the object. Thus value type fields are stored "inside" the object on the heap. Reference type fields are stored as a reference (or pointer) to the referenced object. The memory on the heap are managed by garbage collection. It is a complex subject but the short story is that memory allocated to unused objects on the heap are freed and thus eligble for reuse at regular intervals by the garbage collector.

提交回复
热议问题