Every object that you create has a life cycle, from creation to destruction. When an object is destroyed, its state must be cleaned, and any managed resources used must be reclaimed. And that's where the Garbage Collector comes in. Garbage collection in the Microsoft .NET common language runtime environment, completely absolves the developer from tracking memory usage and knowing when to free memory.
Implementing a proper resource management mechanism by us for our own applications would be a difficult and time taking task. It will surely distract our concentration on the real problems that we are going to solve. Garbage Collector will take all the trouble of doing the resource management, so we will only have to focus our minds on our real problem.
So as I have mentioned before, every object that we create has a life cycle.
So as I have mentioned before, every object that we create has a life cycle.
- First, block of memory will be allocated and that block of memory is big enough to hold the object.
- Then by calling the constructor we initialize the object. Now memory will be converted into an object.
- Object can be then used by our application.
- Object will then arranged for clean up.
- After the cleaning up is completed, the allocated memory for that particular object is reclaimed.
From above things, we can only control the second step and the fourth step. We call the constructor for the second step and we can implement and call the destructor for the fourth step.
So How Does the Garbage Collector Work
The garbage collector runs in its own thread and normally runs automatically, under well defined circumstances. When the garbage collector runs, other threads in an application are halted because the garbage collector may move objects in memory and must update pointers to the correct addresses for these objects.
The steps that the garbage collector would take to reclaim resources are as follows.
The garbage collector runs in its own thread and normally runs automatically, under well defined circumstances. When the garbage collector runs, other threads in an application are halted because the garbage collector may move objects in memory and must update pointers to the correct addresses for these objects.
The steps that the garbage collector would take to reclaim resources are as follows.
- It marks every object as dead; objects are considered dead unless proved otherwise.
- It starts from objects referenced on the stack, marking referenced objects as alive. It performs this recursively; if an object that is already marked as alive references another object, that object is also marked as alive. The garbage collector includes logic to prevent infinite recursion, for example, where there is a circular reference between two objects.
- It checks whether any of the objects that have been marked as dead have a destructor that must be run. Running the destructor is referred to as finalization. Any objects that require finalization are moved to a data structure maintained by the garbage collector called the freachable queue. The freachable queue stores pointers to objects that require finalization before their resources can be reclaimed.
- Objects added to the freachable queue are marked as alive because there is now a valid reference to them; the destructor must be run before their memory can be reclaimed. Objects are normally added to the freachable queue only once.
- Objects marked as alive are moved down the heap to form a contiguous block, defragmenting the heap. References to objects (on the stack and in other objects on the heap) moved by the garbage collector are updated.
- Other threads resume.
- On a separate thread, objects added to the freachable queue are finalized. After an object is finalized, the pointer to that object is removed from the freachable queue. Objects are not removed from memory until the next time the garbage collector runs.
So that's how the garbage collector works in .NET framework. Please feel free to give me your valuable feedback.
Happy Coding.
Regards,
Jaliya
Well done. Thanks a lot for knowledge sharing
ReplyDelete