.NET Hawa

Interview With Garbage Collector

on June 25, 2012

Today we have a guest who will carry the job of shredding all unwatend objects while our applications or our Operating Systems are running i.e., Grabage Collector. Let’s Check with him what he can do….

Tech: Hi Sir, how are you ?

GC: I am doing good I hope….

Tech: Sir we have set of questions about to know about you?

GC: Yes, You can……

Tech: Sir first Ques, What is your birth date ?

GC: I can’t remeber cleary some where around 1959, Created by Jhon Mc Carthy, to solve the problems in LISP programming.

Tech: Ohh, good….. Why he need to create you ?

GC: To overcome the tiredness in manual Heap management.

Tech: what are the areas you will cover?

GC: I will cover each and every area including Operating System Programming also.

Tech: What are the jobs you will take care ?

GC: I will take care about Dangling pointer bugs, Double free bugs, Memory Leaks.

Tech: In what way you will carry out the job ?

GC: By 4 ways Reference Counting, Mark & Sweep, Copying and Compact Collecting.

Tech: Can you breifly explain them ?

GC: My basic implementation of garbage collection uses reference counting: each object is associated with a counter that tells how many other objects refer to it. Say for instance you have a Disk object, every time your system needs another reference to that object (for instance because the DiskPartition object has a reference to the parent Disk object), the reference counter of Disk is incremented. Of course, if programmed manually, this is tedious and bugprone (although in C++ at least, the use of smart pointers can automate this book keeping). Plus GC’s solely based on reference counting will be unable to free self-referencing (or circular) structures, meaning that memory leaks are possible.

A mark-sweep garbage collector traverses all reachable objects in the heap by following pointers beginning with the “roots”, i.e. pointers stored in statically allocated or stack allocated program variables (and possibly registers as well, depending on the GC implementation). All such reachable objects are marked. A sweep over the entire heap is then performed to restore unmarked objects to a free list, so they can be reallocated.

A copying collector copies reachable objects to another region of memory as they are being traversed. Provided the traversal is done in breadth first order, there is a well-known and simple algorithm for performing this traversal without auxiliary storage or recursion. After such a traversal all surviving objects reside in a contiguous region of memory, and all pointers have been updated to point to the new object locations. The previously used region of memory can then be reused in its entirety. Allocation becomes trivial, since all free space is always contiguous.

During the process, the GC builds an object graph to track the “live” objects so that it can update references to any objects that it moves. This means that with a compacting GC, there is no extra level of indirection required in order to move objects around in memory. It also means that programmers cannot rely on the values of references remaining stable during execution of their program, which is one reason why languages such as Java and C# disallow pointer arithmetic on references to garbage collected objects.

A variant of compacting collectors is the generational garbage collector where the memory is partitioned among recently-created objects and longer-lived objects. If an object remains present in the “recently-created” heap for N sweeps of the GC, it is then moved to the “longer-lived” heap. This technique is used by some JVMs and the .NET CLR to reduce the cost of heap-sweeping as the heap grows larger. It is based on the assumption that a long-lived object is less likely to become garbage and thus that the “long-lived objects” heap could be swept with lower frequency.

One of the major advantages of a compacting collector is that it makes allocating from the heap extremely fast and scalable to multiprocessor systems

Tech: Can you give much more explanation about Genrational Garbage Collector ?

GC: In Generational Garbage Collector, 3 generations GC0,GC1,GC2. First GC0 Collects the objects which are created instantaniously and if it not found any references to the object. Object is directly thorwn out of the heap. If the object has much more references and still in use it can be promoted to GC1. GC1 is the 2nd generation, genrally GC1 get Static vairables and constants uses in programmed applications. If the variables are still in use for more time they will send to GC2. GC2 is third generation of me. In thrid generation it is if the object is still in use I will check again for the discardable things in the object.

Tech: Can you tell me what are limitions for you ?

GC: Some of my limitations are:

  1. If the Object is still referenced and not in use I can’t wipe it Off.
  2. If I have less memory, what can I do I will through Exception.

These are main considerable Limitations to me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: