A basic question.. “What is the Garbage Collector?”
The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.”
I wondered why the interviewers changed their facial expressions when I finish saying this answer... :)
It’s because they expect something different from us..
For answering this, we should start with the Managed Heap concept in .Net. It’s not new to us; we all know the basics.
First of all, when we start running our program, the .Net runtime reserves a continuous region of space in memory. This space will be free at this moment. This space region is what we call the Managed Heap.
The Heap also has a pointer, whose job is to point to the region in the heap, where the next object is to be allocated. At this point, since the whole Heap is free, this pointer will point to the base address of the Heap.
Now, consider our program contains a line of code: Object tempObject = new Object();
The “new” operator which we saw in the line has an important role in memory allocation.
The “new” operator first makes sure that the bytes required for allocating the object is free in the Heap. If the required bytes are present, it allocates the memory.
But still, “tempObject” has not become an object yet. This is because .NET runtime has not got any information regarding this entity (“tempObject” is not yet considered an object)
.NET runtime doesn’t know even what type the entity is. By reading the line of code, we now know that the TYPE of tempObject’s is System.Object. But .NET runtime doesn’t know it right now.
All that .NET runtime knows now is, tempObject is some kind of thing for which memory has been allocated.
Then the “new” operator calls the constructor of the class Object, since tempObject is of type Object. This is what we call Object Initialization.
Now since the “new” operator has finished running the constructor of the class Object, it has got all the required information. This is the point, where .NET runtime realizes that tempObject is an object of type System.Object. So now tempObject is not an entity anymore, it’s a fully fledged .NET object.
Now the final task of “new” operator is, it returns the address of this object to the .NET runtime. So now .NET runtime has got the address of this tempObject also, by which it can access this object.
Now as tempObject has been stored in the Heap, the Heap pointer (which we mentioned above) will now point to the next free space in the Heap.
In the same manner, all the objects are stored.
This whole process which is designed by Microsoft, was built under one assumption. The assumption was “Memory space is infinite”.
As we read the process above, .NET runtime keeps on allocating memory on the Heap infinitely, no matter how many objects are present in the program.
As a solution to this, they developed a mechanism which frees up memory where possible, so that their assumption remains true and the system continues to work. This mechanism was named Garbage Collector.
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.