Garbage Collector


Garbage collector is responsible to provide automatic memory management

Why Automatic Memory Management

  • By using automatic memory management:
  • There do not be any problem of insufficient memory
  • Burden on the programmer is reduced i.e. programmer need not write code to perform memory manage tasks
What is Automatic Memory Management

  • Garbage collector has its own engine known as optimizing engine, which runs when required and divides objects into two categories
  • Objects in use [Reachable Objects]
  • Idled objects [Unreachable Objects] (known as Garbage)
  • Objects in use are kept in the memory and idled objects are destroyed from the memory
  • This process is known as Automatic Memory Management
Working Nature of Garbage Collector
  • Garbage collector will divide complete managed heap into three generations internally named as
          Generation 0 
 
         Generation 1
 

         Generation 2

Generation :- A Generation is a portion of memory  from the managed heap
  • Maximum number of generations allowed are only three
  • In general always generation 2 memory size is bigger than the generation  1 memory size and generation 1 memory size is bigger than the generation 0 memory size
  • This can be represented mathematically like
  • Gen2>Gen1>Gen0                or      Gen0<Gen1<Gen2
  • Generally memory to the generations is allotted by the CLR based on application size
  • Garbage collector has a thumb rule that is should place the newly created object only in generation 0
  • GC has no right to place the newly created object into Gen1 or Gen2
  • When application execution starts & application is trying to create a new object then
  • GC will place this object into Gen0
  • Like this newly created object will be placed in Gen0 only until the completion of Gen0 memory
  • When Gen0 is completely filled and application is trying to create a new object then
  • GC will perform an operation known as collection means

  • Collection process contains two phases
             Marking Phase
             Compact Phase
  • During Marking phase GC will examines all the object present in Gen0, identities idled objects & objects in use
  • During Compact Phase Idled objects are destroyed Objects in use are transferred to next generation so that Gen0 will become empty
  • Newly created object is placed in Gen0
  • Like this, after performing so many collections of Gen0, Gen1 will be completely filled
  • When Gen0 and 1are completely filled and application is trying to create a new object then
  • GC will perform an operation known as collection means
  • Examines all the objects present in Gen0 and 1, identifies idled objects and objects in use
  • Idled objects are destroyed or placed in finalization queue
  • Objects in use are transferred to next generation so that Gen0 and 1will become empty
  • Newly created object is placed in Gen0
  • Like this, after performing so many collections of Gen0, Gen1, Gen2  will be completely filled
  • When Gen0,1,2 are completely filled and applications is trying to create a new object then
  • GC will perform collection of 3 Gens 0, 1, 2 i.e.
  • Examines all the objects in 3 Gens
  • Divides the objects into idled objects and objects in use
              Idled objects are destroyed or placed in finalization queue
             Objects in use are adjusted in Gen2 & 3 also in 1 if necessary
  •  Newly  created object is placed in Gen0

 



  • To perform the collection of Generation 0 GC will take 1/10th of a nanosecond time which is less than a page cycle
  • Microsoft discovered that searching for an idled object with in a small portion of memory is much faster as compared with searching for an object within the complete managed Heap, because of this only managed Heap is divided into 3 generations
  • Complete GC is programmable
  • Automatic memory management in .NET is efficient because of maintaining three generation, searching for an Idled object within the small portion of memory will be much faster than searching within the complete managed heap and is most efficient approach
  • When Garbage Collector will perform Collection ?
         GC Will perform the collection when there is no enough memory in Generation0

 




No comments:

Post a Comment

Translate