Thursday 21 June 2012

Memory Management - Value Types and Reference Types

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91
 
Summary:
  • The main job of Stack is to keep track of what's executing in our code in a last in first out basis (LIFO).
  • The main job of Heap is to keep track of our objects, data and information.
  • An Orphan Object in Heap is an object, candidate for garbage collection, with no pointer to it.
  • A Reference Type always goes on the Heap.
  • When we are using Reference Types, we're dealing with Pointers to the type, not the thing itself.
  • Value Types and Pointers always go where they were declared. more complex.
  • A Value Type declared within the body of a method, will be placed on the stack.
  • A Value Type declared outside of a method but inside a Reference Type, will be placed within the Reference Type on the Heap.
  • A pointer to a Reference Type declared whithin a method body, will be placed on the stack.
  • A pointer to a Value Type declared whithin a method body, will be placed on the stack.
  • Items are first get deleted from Stack, then orphan items are deleted from Heap using GC
  • When we're using Value Types, we're using the thing itself.