.NET Memory Management - Part Two

Introduction

Garbage Collector (GC) is an important part of .NET Memory Management. GC is required for an automatic memory cleanup.
.NET Garbage Collector identifies some potential sources of references and all these references are called Root references or GC root. They identify these GC roots to make sure that the objects which are no longer in use clear.

GC roots or Root references are given below-

  1. Global/Static Object References
  2. CPU registers
  3. Object Finalization References.
  4. Interop References.
  5. Stack References

GC collects Root references, marks the live objects and cleans up the objects, not in use. GC collection code is given below-

  1. public void GCCollection  
  2. {  
  3.     var gcRoots = GetAllGCRoots();  
  4.     foreach(var root in gcRoots) {  
  5.         Mark(root);  
  6.         CleanUp();  
  7.     }  
  8. }  
Here, the Mark method adds or marks the live object, which is still in use.

Static Object

Static or Global objects are the sources of Root Reference or GC root. Whenever method, property, or an event is created by using static keyword, it creates Global instance.

Static objects are accessible by all the threads in an app domain, unless they are marked with the [ThreadStatic] attribute. They are never Garbage Collected because they are root references in themselves. Static objects remain loaded in the memory for a longer time.

That is the same case with static event. It remains in the memory until the event subscription is removed.

Thread Static

ThreadStatic is used to prevent the multiple threads, accessing a common set of static members.

Example
  1. [ThreadStatic]   
  2. public static int count=0; 
Here, this code describes that the value of each static field will be unique for each thread. It will be thread safe. It will not be shared among the thread.

Conditions for the Garbage Collection

Garbage Collection occurs, when one of the conditions, given below are true- 
  1. The system has a low physical memory.
  2. GC.Collect method is being called.

Different Phases of Garbage Collection

Different phases of Garbage Collection in .NET are given below-

Marking Phase - This phase of Garbage Collector finds and creates a list of all the live objects.

Relocating Phase - This phase of Garbage Collector updates the references to the objects, which will be compacted.

Compacting Phase - This phase of Garbage Collector reclaims the space occupied by the dead objects and compact the serving objects.

Way to Identify Live Objects by Garbage Collector

The different ways to identify live objects by Garbage Collector are-
  1. Stack Root
  2. Garbage Collection Handles
  3. Static Data

Question 1 - See the class, given below-

  1. public class Test  
  2. {  
  3.     int x;  
  4.     int y;  
  5. }  
Where would  the memory be allocated? Both are the primitive data types. Will it be allocated on Stack or Heap?

Answer - This is the class definition and class definition data is stored on Heap. Whenever class is being instantiated, objects are stored in Heap.

When function gets called, the function's primitive data types are stored in Stack.

Also, if the function has a reference to objects, these objects will be created on Heap but, the reference variables to those objects will create Stack.

When the function exists, all the reference variables stop pointing to Heap.

Conclusion

Thus, these are the different ways operations are done by Garbage Collector. I will explain another topic -- memory management  -- in the next part. 
Ebook Download
View all
Learn
View all