Take the 2-minute tour ×

Possible Duplicate:
difference between destructor and garbage collector

Recently i asked this question, now as per the answers I came to the conclusion, that

Ok, let me check whether I got the concept, GC is used to just check whether any disposing of the object is required, and if any it will call the destructor, GC cannot destroy anything on its own, and Destructor on the other hand, will destroy the object at the end of the class, and destructor cannot check for the memory issues, it just destroys everything at the end. am I right?

I commented there, but didn't got the reply, so I tried to post new question here. can anyone reply, is my conclusion right

share|improve this question
You added that comment 9 minutes ago. Please be more patient. –  Jon Skeet Feb 27 '12 at 10:38
add comment

marked as duplicate by Jon Skeet, Wouter de Kort, Fadrian Sudaman, L.B, Henrik Feb 27 '12 at 10:44


1 Answer

up vote 1 down vote accepted

No, the destructor (~myClass) just does the stuff you want him to do when an object is no longer needed, like freeing resources (files, connections, ...). Its the opposite of the constructor (myClass), where you set up your object (getting connections, etc). The GC removes all pointers to the myClass-object when it goes out of scope or the destructor was called (I think). So the location where your object was before in memory can be allocated again.

share|improve this answer
Surprisingly, the GC doesn't know anything about most objects that go out of scope. Instead, it primarily works by copying everything it can find to which any sort of reference exists to a new area of memory and then declaring that all of the memory that was formerly used to hold objects of the generation just collected is available for reuse. There are some corner cases like WeakReferences, monitor locks, pinned objects, registered finalizers, etc. but for the most part the garbage collector neither knows nor cares how many abandoned objects exist. –  supercat Mar 2 '12 at 0:00
add comment
