#12 Root-GC

Garbage Collector(GC) has to remove the object that are no longer referenced in program.It has to clean up the heap memory so that it can free up space for objects which are newly created.

Root:

Every object’s start/base address in heap is referenced to GC by an element called Root.So Root is nothing but an address where the object is located in Heap.

Usage of Root:

1.Whenever GC starts cleaning the Heap,it starts by assigning the default value of 0 to Sync index (Every object has type object pointer and sync index along with its fields).

2.It then compares the Root(Contains all address of object that are in Heap) with all objects in Heap.Whenever there is a match with Root and Object in Heap,it marks the Sync index to 1.

3.GC has then checks for objects which has sync index to 0.These objects are no longer of use and are removed from Heap.

#11:Examining Generators Via CLR-WINDGB/NSTD

We can Examine the Generator Type(https://rangeshcode.wordpress.com/2014/08/03/10types-of-generators-gc/)  of Application using WinDbg/NSTD.

Steps(NSTD/WinDBG):

1 .symfix

2. .reload

3.g(Run exe)

4.ctrl+Break/Break -Break in Exception

5. .loadby sos clr (.net 4.0 >Above)

6.~0s(First Stack)

7. !eeheap -gc

8.To know the generation use sosex(.load sosex) and use !gcgen addreess.

 

Capture

 

 

#10:Types of Generators-GC

Garbage Collector has to decide Cleaning Process on Heap, based upon the lifetime of  objects. GC Categorizes the LifeTime with Generations,

Generation 0:Short Lived Objects.(Temp Variables)

Generation 1:Medium Lived Objects

Generation 2:Long Lived Objects.(static variables)

Generation 0 and 1 Are called Ephemeral(Short Lived) Segments.

Every Objects start their cycle with Generation 0,Upon Garbage Collection,if they survive the Clean,then these objects are moved to next generation.

 

#5 Ref and Out Parameters


int NormalVaribale = 1;
 int referencevariable = 1;
 int outvaraibale = 1;

Normalmethod(NormalVaribale);
 referencemethod(ref referencevariable);
 outmethod(out outvaraibale);
 Console.WriteLine("Normal Variable"+NormalVaribale);
 Console.WriteLine("(REF)Reference Variable"+referencevariable);
 Console.WriteLine("(out) Out Varaible"+outvaraibale);

ref :Value must be Initialized before it is passed to the method. Reference is being passed to the method,so the value changed will get reflected./*It has got Nothing to do with value type and reference type both of these can use ref*/

out:Value need not be Assigned,but has to be defined before the method exists.

Note:Even though you have initialized before passing to method,you got to assign value before the method exists.

 

#4 Static Constructors

1.Static Constructors cannot have Access Modifiers  or have parameters.

2.They are initialized before  any static members are referenced.They are the First One to get Initialized.

3.A non static class can have static constructor.

class Test
{ 
static Test() //cannont have access modifier
{
Console.WriteLine("Hello World");
}
}

 

 

 

 

#3 Decorator Pattern

When to Use Decorator Pattern?

Add New Responsibility(functionality) to the class,without breaking the existing functionality.

Usage:

1.Component as abstract class/Interface.

2.Concrete Components Implementing the Components.

3.Decorator as abstract class/Interface with Component as member initialized via constructor.

4.Concrete Decorator Implements Decorator.

Consequence:
Functionality are added at runtime,Hard to debug(Since object implements both Concrete Component and Concrete Decorator).

 Source Code:https://github.com/rangesh-/Design-Patterns/tree/master/Design%20Patterns/Decorartor%20Pattern