• Non-object types
• Stored in memory “stack”
• int, long, char, byte,float, double,decimal,bool etc.
• Structs,Enumerations
Reference Types
• Object types
• Stored in memory “heap”
• Variables are “pointers” to memory location
Memory Allocation
The Common Language Runtime allocates memory for objects in two places: the stack and the heap. The stack is a simple first-in last-out memory structure, and is highly efficient. When a method is invoked, the CLR bookmarks the top of the stack. The method then pushes data onto the stack as it executes. When the method completes, the CLR just resets the stack to its previous bookmark—“popping” all the method’s memory allocations is one simple operation!
In contrast, the heap can be pictured as a random jumble of objects. Its advantage is that it allows objects to be allocated or deallocated in a random order. As we’ll see later, the heap requires the overhead of a memory manager and garbage collector to keep things in order.
void CreateEmployee() { Employee myEmployee = new Employee(); // Employee is a class Employee myEmployee1; myEmployee1 = myEmployee; //Since this is being passed as value type a copy of reference variable will be //passed to DoSomeWork which will point to the same employee objct so //any modification to the actual employee object will be visible here. DoSomeWork(myEmployee); } void DoSomeWork(Employee employee) { employee.WorkCompleted = True; //Since this is modification to the actual object this will be visible in the calling function employee = null; //Since this is modification to the local variable this will not impact anything in the calling function. }In this method, we create a local variable myEmployee that references an Employee object. The local variable is stored on the stack, while the Employee itself is stored on the heap. Also in the third line we are saying that local variable myEmployee1 reference to the same Employee object, so modification to the Employee object will be reflected from the both references myEmployee as well as myEmployee1
Stack | Heap
|
myEmployee Reference---- |----->Employee object
|
|
The stack is always used to store the following two things:
• The reference portion of reference-typed local variables and parameters
• Value-typed local variables and method parameters (structs, as well as integers, bools, chars, DateTimes, etc.)
The following data is stored on the heap:
• The content of reference-type objects.
Memory Disposal
Once CreateEmployee has finished running, its local stack-allocated variable, myEmployee, will disappear from scope and be “popped” off the stack. However, what will happen to the now-orphaned object on the heap to which it was pointing? The answer is that we can ignore it—the Common Language Runtime’s garbage collector will catch up with it some time later and automatically deallocate it from the heap. The garbage collector will know to delete it, because the object has no valid referee.
The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used, however, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.