Stack and heap in sharp

Possible duplicate:
Why are structures stored on the stack and classes stored on the heap (.NET)?

Can someone tell me how memory allocation is performed, which object should be stored on the stack and which should be on the memory heap?

+4
source share
2 answers

3 rules of thumb:

  • Objects are stored on the heap . These include instances of reference types and boxed value types.
  • Local variables and parameters are stored on the stack . For local value-values, this means that the value itself is stored on the stack. For local reference types, only the link will be used in the stack (Edit: Exceptions marked by Eric Lippert - value type locales, closed above external variables, value types of iterator-block).
  • Fields are saved where the containing instance is located . For example, a field such as a class value will be stored on the heap. The reference part of the field of the reference type of the structure declared as local stored on the stack will also be on the stack.
+5
source

In the Microsoft implementation of the C # compiler and CLR, value types are stored on the stack when this value is a temporary value, a local variable, or a formal parameter, which is neither a private external variable of the anonymous method, nor an iterator block.

Of course, why store things on the stack if you don't need it? Some local variables of type value never hit the stack; they remain in registries for life.

Other value type values ​​are stored in heap value types, entered values ​​of type of reference type, etc.

Value types, of course, can be stored neither on the stack, nor in the register, nor in the managed heap; they can be stored in unmanaged memory using some completely different memory manager that does not control the CLR.

(And, of course, note that using "the" in a "stack" is a subtle misconception; there can be many stacks in a process. There shouldn't be just one.)

All of these are implementation details and are subject to change without notice.

In addition, it is obvious that information allocated by the stack allocation declaration is allocated on the stack.

For more information about this section, see my articles:

http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx

Why does it bother you? Runtime manages all of these details for you, so you don't need to worry about it. Are you just curious, or does this lead to a larger question?

+12
source

All Articles