While thinking of this question few things are coming in my mind ~ How .Net reclaims objects and memory used by an application ? So the answer is Garbage Collector
Memory management is the main part of CLR and Garbage Collector does the job for CLR. Garbage Collector attempts to reclaim garbage memory or memory occupied by objects those are no longer use by the application.
Advantages of Garbage Collector
When Garbage Collection Occurs ?
So now you know how Garbage Collection performed in an application but this was the over view. Garbage collector maintains some generations to manage objects. So now the question comes into the mind :
How Garbage Collector reclaims memory ?
The Garbage Collector checks whether the object in heap is still in use or it's dead or not being used by the application. If it's not being used by the application then Garbage Collector claims that memory. So now the question comes in the mind How GC knows whether this object is in use or not ?
So each application has some set of application roots and these roots are maintained by CLR. These are kind of pointers those points to the storage location and those location refer objects. If those objects are null then GC reclaims that.
Application Roots: Global objects, static pointers and local object pointers that resides under thread stack are considered as application roots. From these roots GC identifies the dead objects.
Now we can see that Application roots are pointing to four objects : Object c, object D, Object F and Object H but Object C is pointing towards Object G as well. So these objects are still referring in the application but other objects are dead or not referring in the application. GC marked those objects as Garbage. So this was the first phase of GC known as Marking Phase.
Second stage is Relocation and Compaction : In this GC traverse linearly through the Heap, looking for memory blocks of Garbage objects and consider it as free memory. Now shifts all the non garbaged objects down in the heap and update the references to the objects using memcpy method. Lets look over the new graph after this phase -
GC only occurs when managed heap is full. So that's how GC algorithm works. Now GC also have Generation concepts to improve performance.
Generation 0: It's a youngest generation and contains all newly created objects.
Generation 1: Objects, who survives after next GC occurs, then they promote to Generation 1.
Generation 2: It's for long lived objects like objects those exists through out the application like static objects.
I will talk more about Generations in my Next article.
Hope you enjoyed this article.
Memory management is the main part of CLR and Garbage Collector does the job for CLR. Garbage Collector attempts to reclaim garbage memory or memory occupied by objects those are no longer use by the application.
Advantages of Garbage Collector
- Allocates memory efficiently in Managed Heap
- Reclaims objects that are no longer be used by the application and keeps the memory available for other objects.
- You do not need to worry about memory. You just write your code GC will take care of the things.
- Provides safe memory so that an object can not use content of another one.
When Garbage Collection Occurs ?
- When system has low physical memory
- When a process is initialized run time reserves a contiguous region of address space that initially has no storage allocated for it. This region is known as managed heap. The heap maintains a pointer - NextObjPtr. This pointer indicates where the next object is to be allocated within the heap. Initially, this pointer is set to the base address of the managed heap.
So now you know how Garbage Collection performed in an application but this was the over view. Garbage collector maintains some generations to manage objects. So now the question comes into the mind :
How Garbage Collector reclaims memory ?
The Garbage Collector checks whether the object in heap is still in use or it's dead or not being used by the application. If it's not being used by the application then Garbage Collector claims that memory. So now the question comes in the mind How GC knows whether this object is in use or not ?
So each application has some set of application roots and these roots are maintained by CLR. These are kind of pointers those points to the storage location and those location refer objects. If those objects are null then GC reclaims that.
Application Roots: Global objects, static pointers and local object pointers that resides under thread stack are considered as application roots. From these roots GC identifies the dead objects.
Now we can see that Application roots are pointing to four objects : Object c, object D, Object F and Object H but Object C is pointing towards Object G as well. So these objects are still referring in the application but other objects are dead or not referring in the application. GC marked those objects as Garbage. So this was the first phase of GC known as Marking Phase.
Second stage is Relocation and Compaction : In this GC traverse linearly through the Heap, looking for memory blocks of Garbage objects and consider it as free memory. Now shifts all the non garbaged objects down in the heap and update the references to the objects using memcpy method. Lets look over the new graph after this phase -
GC only occurs when managed heap is full. So that's how GC algorithm works. Now GC also have Generation concepts to improve performance.
Generation 0: It's a youngest generation and contains all newly created objects.
Generation 1: Objects, who survives after next GC occurs, then they promote to Generation 1.
Generation 2: It's for long lived objects like objects those exists through out the application like static objects.
I will talk more about Generations in my Next article.
Hope you enjoyed this article.
Thanks for sharing informative article. Download Windows 7 ultimate for free from getintopc. It helps you to explore full functionality of windows operating system.
ReplyDeleteThanks for sharing this blog..Appreciate your efforts..Keep writing..
ReplyDeletefull stack developer course in chennai
full stack developer course in chennai with placement
full stack developer course with placement
full stack developer course near me
full stack developer course near me
full stack developer course fees
full stack developer course in chennai BITA Academy
Uipath training in chennai
Uipath training in chennai BITA Academy
blue prism training in chennai
blue prism training in BITA Academy
rpa training in chennai
rpa training in velachery
rpa training in chennai velachery
Excellent Blog..has lots of information..Completely impressed..
ReplyDeletePlacement Training in Chennai
Placement Training in Chennai BITA Academy
Linux Training in Chennai
PERL Training in Chennai
Security Testing Training in Chennai
Security Testing Course in Chennai
Selenium Training in Chennai
Selenium Course in Chennai
JMeter Training in Chennai
Appium Training in Chennai
Thank you for sharing this article. Quickbooks is one of the advanced accounting software to handle business related transactions. You can install quickbooks on mac also. For more information, visit our website.
ReplyDeleteok
ReplyDeleteGarbage collection" refers to the automatic memory management process in computer programming. It involves the identification and removal of unused or "garbage" memory objects by the programming language's runtime environment. Minimum Spaces Needed This process helps prevent memory leaks and ensures efficient memory.
ReplyDelete