The .NET framework provides automatic memory management GC allocates memory on Managed heap or Stack MyObject myObj =MyObject();//allocates memory The framework takes care of figuring out when are no longer being used and reclaims their memory for reuse in the system
Memory Allocation value types get allocated on Stack Reference Types get allocated on Heap CLR will allocate memory based on known storage lifetime .-valid content Types of Locations: Heap - >long lived objects stores Stack - >short lived object stores Register-> “ “ ” ”
The managed heap allocates a segment of memory to store managed objects In managed heap GC will allocate and reclaim memory. Frequency - result of the volume of allocations and the amount of survived memory on the managed heap. In native heap programmer has to do it Types of Heaps: Large Object Heap (LOH): contains 85kb and large. In LOH usually Arrays, collection. Small Object Heap (SOH): contains lesser size than 85k.
Generations, Survival and Promotions Objects are maintained as 3 Generations . Generation 0- this generation contains short lived and youngest objects. Example- temporary variable Objects. More frequently object reclamation happens in this generation. Survived objects would promote to Generation 1. Generation 1: this generation contains short lived objects and serves as a buffer between short lived and long lived objects. Survived objects are promoted to generation 2. Generation 2: this generation contains long lived objects. Example: A static variable lives up to duration of process. Survived objects remains in generation 2.
Types of Garbage Collection WorkStation Garbage Collection Modes of GC: a. Concurrent Garbage Collection b. Background Garbage Collection c. Non-Concurrent Garbage Collection Server Garbage Collection a . Background Garbage Collection b. Non-Concurrent Garbage Collection
Server Garbage Collection intended for server applications which have multiple processors faster than workstation - multiple garbage collection threads work -> mode all threads run on Highest priority . resource intensive-> ex. You have 10 and 8 processor there will be 80 dedicated GC threads Background Garbage collection (BGC) is enhancement and replacement after version 4-gen 2
WorkStation Garbage Collection This works in client work station and standalone computer triggers on user’s thread will run on normal priority thread must compete with others for CPU time Workstation GC can be Concurrent or non-concurrent GCs It This is always used on a computer that has one processor By default concurrent mode is disabled by CLR.
How to Force Garbage Collection sample code 1 2 3 4 5 StreamWriter stream = File.CreateText (“temp.dat”); stream.Write (“some test data”); GC.Collect (); GC.WaitForPendingFinalizers (); File.Delete (“temp.dat”);
quiz class HappyGarbage01 { public static void main(String args []) { HappyGarbage01 h = new HappyGarbage01(); h.methodA (); /* Line 6 */ } Object methodA () { Object obj1 = new Object (); Object [] obj2 = new Object[ 1 ]; obj2[0 ] = obj1; obj1 = null ; return obj2[ ]; } } Where will be the most chance of the garbage collector being invoked? A. After Line 9 B. After Line 10 C. After Line 11 D. Garbage collector never invoked in methodA ()
quiz class HappyGarbage01 { public static void main(String args []) { HappyGarbage01 h = new HappyGarbage01(); h.methodA (); /* Line 6 */ } Object methodA () { Object obj1 = new Object (); Object [] obj2 = new Object[ 1 ]; obj2[0 ] = obj1; obj1 = null ; return obj2[ ]; } } Where will be the most chance of the garbage collector being invoked? A. After Line 9 B. After Line 10 C. After Line 11 D. Garbage collector never invoked in methodA () ANSWER IS D.Garbage collector never invoked in methodA ()
Explanation Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6. Option A is wrong. Because the reference to obj1 is stored in obj2[0]. The Object obj1 still exists on the heap and can be accessed by an active thread through the reference stored in obj2[0]. Option B is wrong. Because it is only one of the references to the object obj1, the other reference is maintained in obj2[0]. Option C is wrong. The garbage collector will not be called here because a reference to the object is being maintained and returned in obj2[0].