Wednesday, April 20, 2016

Garbage
collection is automatic process performs by Garbage collector (which lives in
CLR) to free up memory of objects which are no longer in used.

Every time you create a new object, the common language runtime allocates
memory to store that object. But at some point that object may be no longer
needed by your application. So that garbage collector deallocates memory of
this type of objects, so that application can store new objects into memory. If
memory is not deallocated then sooner your application will run out of memory.

Interview Questions related to Garbage Collector

A. How does Garbage
collection works?Every time a garbage collection is performed,
the garbage collector basically looks at the memory and frees up memory which
is no longer needed, that is, memory which is occupied by ‘dead objects’.

B. How does it know
when an object is ‘dead’?

An object is dead if it is unreachable by your
code. The obvious example is a local variable inside a method. There is no way
for your code to access that variable once the method has returned, so it
becomes ‘dead’.

C. How often does the
garbage collector perform a garbage collection?

There are three ways a garbage collection can
be triggered.

Firstly, if your system has low physical
memory, this can trigger a garbage collection.

Secondly, a threshold is defined which
indicates an acceptable level of memory on the heap which can be used by
allocated objects. If this threshold is surpassed, then a garbage collection is
triggered.

Finally, a garbage collection can explicitly be
triggered by calling the GC.Collect method. Only under very rare circumstances
is this ever required.

D. What are generations
in garbage collector?

Generation define the age of
the object.

E. Generations of Garbage collections? / How many generations
are there in Garbage collector?

The heap is organized into generations so it
can handle long-lived and short-lived objects. Garbage collection primarily
occurs with the reclamation of short-lived objects that typically occupy only a
small part of the heap. There are three generations of objects on the heap:

Generation 0. This is the youngest generation and contains
short-lived objects. An example of a short-lived object is a temporary
variable. Garbage collection occurs most frequently in this generation.Newly allocated objects form a new generation
of objects and are implicitly generation 0 collections, unless they are large
objects, in which case they go on the large object heap in a generation 2
collection.Most objects are reclaimed for garbage
collection in generation 0 and do not survive to the next generation.

Generation 1. This generation contains short-lived objects
and serves as a buffer between short-lived objects and long-lived objects.

Generation 2. This generation contains long-lived objects.
An example of a long-lived object is an object in a server application that
contains static data that is live for the duration of the process.

Objects that are not de-allocated in a garbage
collection are known as survivors, and are promoted to the next generation.
Objects that survive a generation 0 garbage collections are promoted to
generation 1; objects that survive a generation 1 garbage collection are promoted to generation 2; and objects that survive a generation 2 garbage
collection remain in generation 2.

F. Advantages of
garbage collector

1. No need to take care of free-up memory while
developing applications

2.Allocates memory of objects efficiently on
managed heap.

3.Release memory of objects which are no longer
in used in applications.

G. Phases of garbage
collector

1.Marking Phase - In this phase garbage collector finds and creates list of all live objects.

2.Relocating Phase - In this phase garbage collector updates references of objects that will be compacted.

3.Compacting Phase - In this phase frees-up memory of objects which are no longer in used & compact surviving objects.

H. Does garbage
collector clean unmanaged objects?

No.

I.When we define
clean up destructor, how does it affect garbage collector?

If you define clean up in
destructor garbage collector will take more time to clean up the objects and more and
more objects are created in Gen 2