Memory Management

.Net Memory Management is...


 * Process: Isolation Boundary to protect memory from harm by bad apps.  Also defines access rights.
 * Application Domain: Isolation boundary similar to processes except that application domains are housed by processes which can have a number of app domains. A .Net artifact.
 * Context: Boundary that groups objects with the same usage rules
 * Stack (maintained by the App Domain)
 * Holds value types (except for object members), pointers to reference types and method call parameters.
 * Higher performance than heaps
 * Last-In-First-Out
 * Works from high memory to low with a pointer pointing to the next free slot


 * Managed Heap (maintained by the App Domain)
 * Holds reference types (objects)
 * Garbage collection
 * Misses Unmanaged resources like file handles, network connections and database connections
 * Use a destructor or System.IDisposable for these.
 * Higher performance than unmanaged heaps
 * Garbage collector compacts making a pointer to next free memory still possible and keeps all live objects close together.
 * In and out random – depends on life-times of objects which is unrelated to any other object
 * Works from low memory to high with a pointer pointing to the next free slot.


 * Boxing
 * Provides a bridge between value-types and reference-types by permitting any value of a value-type to be converted to and from type object. Allows value-types to be treated as objects.