C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




What is the logic / algorithm of Garbage Collector?


Posted Date: 20 Feb 2004      Total Responses: 3

Posted By: Manoj Oommen       Member Level: Bronze     Points: 2


We all know there is a grabage collector.But how does the garbage collector work.?
Thanks mean while Mr gopalakrishnan for answering my earliar query.:).




Responses

Author: Gajalakshmi    02 Mar 2004Member Level: BronzeRating:     Points: 2
The Memory management in the COM Based Architectures is Reference Counting. But In Case of .NET, It Differs entirely. It Is Reference Tracing.

Reference Counting:
Whenever an Object gets created in the Heap, an Reference count gets increased and When it gets deleted, the reference count of the object is decreased. It is the Developers responsibility 'to ensure the proper release of the allocated resources.

Reference Tracing:
In .NET, Automatic Memory management takes place by Reference Tracing and the Garbage Collector is responsible for handling this.

Lets first look at the object allocation in Heap:

Heap's storage is classified as follows:
1. Free space
2. Reserved Space


Whenever an Object gets allocated in the heap, the following Rules have to be followed.

1. Memory allocation takes place in a Continuous Range of the Free Space.
2. The order of objects in memory remains the order in which they were created, for good locality.
3. There are never any gaps between objects in the heap.
4. The Oldest Objects are in the lowest address.

GC Algorithm:
The Reference Tracing does not occur once the object goes out of scope. The GC starts its work] when the memory in the heap is full.

The GC checks for each and every object if it is "reachable".
To determine reach ability, the GC starts at all root objects - which are basically all static (shared in VB.NET) members and all in-scope local variables - and traverses the complete object reference graph. Each object which is encountered by the GC on its way is marked as alive.

Objects in the Heap will be in any of the two states:
LIVE
DEAD

In a second pass, all non-marked objects are destroyed, their resources freed, and finally the heap is compacted again to prevent memory fragmentation.




Author: Rupreet Singh    02 Mar 2004Member Level: BronzeRating:     Points: 2
Just to add, here is a link which will tell you in detail about the working of GC.

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/gci2/

Hope this helps

Regards,
Rupreet Singh


Author: asdf    08 Aug 2008Member Level: SilverRating:     Points: 0
• Garbage collector
To destroy “no longer referenced objects” by destructor(finalization) to make memory free.
Whenever the memory full the destructor will be called by Garbage collector at regular interval to clean up the memory.
But notable thing is Destructor take more resources like unwanted memory and Finalization Act in separate thread like again eating resources



Post Reply
You must Sign In to post a response.
Previous : what is difference between String and string(data type) in c#?
Return to Discussion Forum
Post New Message
Category: .NET



dotNet Slackers   BizTalk Adaptors    Web Design

it support

Contact Us    Privacy Policy    Terms Of Use