What Is Garbage Collection?


  • Memory Management technique. 
  • Process of freeing objects, No longer referenced by the program 


Why Garbage Collection?


  • Free unreferenced objects. 
  • Combat heap fragmentation. 
  • Relieves programmer from manual freeing the memory.
  • Helps in ensuring program integrity. 


Disadvantages Of Garbage Collection


  • Extra Overhead.
  • Less control over scheduling of CPU time

The Garbage Collection Algorithm in .NET

  • Microsofts .NET common language runtime requires that all resources be allocated from the managed heap. 
  • Managed heap has a pointer NextObjPtr which indicates where the next object is to be allocated within the heap. The Garbage Collection Algorithm 
  • The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application. 
  • If such objects exist, then the memory used by these objects can be reclaimed. 
  • If no more memory is available for the heap, then the new operator throws an OutOfMemoryException.

How GC knows if app. Is using object or not

  • Every application has a set of roots. 
  • Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null. 
  • The list of active roots is maintained by the just-in-time (JIT) compiler and common language runtime, and is made accessible to the garbage collector's algorithm. 
  • GC constructs graph of all reachable objects based on assumption that all objects are garbage 
  • The GC walks through the heap linearly, looking for garbage objects (free space now). • GC then shifts non garbage objects down in the memory, removing all the gaps in the heap.




Finalization

  • By using finalization, a resource representing a file or network connection is able to clean itself up properly when the garbage collector decides to free the resource's memory. 
  • When the garbage collector detects that an object is garbage, the garbage collector calls the object's Finalize method (if it exists) and then the object's memory is reclaimed. 
  • Finalize is very different from destructors. 
  • Finalizable objects get promoted to older generations, which increases memory pressure. 
  • All objects referred to directly or indirectly by this object get promoted as well. 
  • Forcing the garbage collector to execute a Finalize method can significantly hurt performance
  • Finalizable objects may refer to other (non-finalizable) objects, prolonging their lifetime unnecessarily. 
  • You have no control over when the Finalize method will execute. The object may hold on to resources until the next time the garbage collector runs. 
  • If you determine that your type must implement a Finalize method, then make sure the code executes as quickly as possible. Avoid all actions that would block the Finalize method 
  • Each entry in the queue points to an object that should have its Finalize method called before the object's memory can be reclaimed. 
  • At this point, the garbage collector has finished identifying garbage. 
  • There is a special runtime thread dedicated to calling Finalize methods. 

Garbage Collection in .NET Framework

What Is Garbage Collection?


  • Memory Management technique. 
  • Process of freeing objects, No longer referenced by the program 


Why Garbage Collection?


  • Free unreferenced objects. 
  • Combat heap fragmentation. 
  • Relieves programmer from manual freeing the memory.
  • Helps in ensuring program integrity. 


Disadvantages Of Garbage Collection


  • Extra Overhead.
  • Less control over scheduling of CPU time

The Garbage Collection Algorithm in .NET

  • Microsofts .NET common language runtime requires that all resources be allocated from the managed heap. 
  • Managed heap has a pointer NextObjPtr which indicates where the next object is to be allocated within the heap. The Garbage Collection Algorithm 
  • The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application. 
  • If such objects exist, then the memory used by these objects can be reclaimed. 
  • If no more memory is available for the heap, then the new operator throws an OutOfMemoryException.

How GC knows if app. Is using object or not

  • Every application has a set of roots. 
  • Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null. 
  • The list of active roots is maintained by the just-in-time (JIT) compiler and common language runtime, and is made accessible to the garbage collector's algorithm. 
  • GC constructs graph of all reachable objects based on assumption that all objects are garbage 
  • The GC walks through the heap linearly, looking for garbage objects (free space now). • GC then shifts non garbage objects down in the memory, removing all the gaps in the heap.




Finalization

  • By using finalization, a resource representing a file or network connection is able to clean itself up properly when the garbage collector decides to free the resource's memory. 
  • When the garbage collector detects that an object is garbage, the garbage collector calls the object's Finalize method (if it exists) and then the object's memory is reclaimed. 
  • Finalize is very different from destructors. 
  • Finalizable objects get promoted to older generations, which increases memory pressure. 
  • All objects referred to directly or indirectly by this object get promoted as well. 
  • Forcing the garbage collector to execute a Finalize method can significantly hurt performance
  • Finalizable objects may refer to other (non-finalizable) objects, prolonging their lifetime unnecessarily. 
  • You have no control over when the Finalize method will execute. The object may hold on to resources until the next time the garbage collector runs. 
  • If you determine that your type must implement a Finalize method, then make sure the code executes as quickly as possible. Avoid all actions that would block the Finalize method 
  • Each entry in the queue points to an object that should have its Finalize method called before the object's memory can be reclaimed. 
  • At this point, the garbage collector has finished identifying garbage. 
  • There is a special runtime thread dedicated to calling Finalize methods.