C# .NET - Garbage Collector - Asked By Naresh Kumar on 23-Mar-10 03:11 AM

Hi All,

Garbage collector will run on the heap at runtime and frees the objects memory which does not have references with stack.
Suppose I have a code
void MyMethod
{
     MyCLass myObject = new MyClass();
     myObject.SomeProperty = "Some Text";
     some code....
}

Now here the object mYObject will be stored in Heap and the refence will be stored in stack.
If the control is at the end of the method myObject will be out of scope refence will be deleted from stack.
What will happen in the below two scenarios
void MyMethod
{
     MyCLass myObject = new MyClass();
     myObject.SomeProperty = "Some Text";
     some code....
     myObject.dispose();
}
void MyMethod
{
     MyCLass myObject = new MyClass();
     myObject.SomeProperty =Te "Some xt";
     some code....
     System.GC.Collect();
}

Please let me know what happens when I run the GC.Collect or dispose method on the object.

Thanks in advance.
samjayander thiagarajan replied to Naresh Kumar on 23-Mar-10 03:38 AM
Hi,

The statement, MyObject.dispose() give the object to the GarbageCollector.  You should have implemented code to clean up MyObject.

The statement GC.collect forces the GC to collect all objects which no longer has references. I hope in your example, the object MyObject will not be Garbage collected with GC.Collect unless the reference is broken.

Regards,
Sam.
Sagar P replied to Naresh Kumar on 23-Mar-10 04:01 AM
Dispose is the best way we do clean our unallocated resources and yes not to forget we do not get the hit
of running the Garbage collector twice.
System.GC.Collect() forces garbage collector to run.This is not recommended but can be used if
situations arises.

Adam Houldsworth replied to Naresh Kumar on 23-Mar-10 04:36 AM
Calling Dispose on an object will usually release any unmanaged resources it has underneath - it does nothing with the Garbage Collector.

I have a blog post about using Dispose:
http://adamhouldsworth.blogspot.com/2010/02/idisposable-disposable-pattern.html

Calling GC.Collect will fire the garbage collector, but because your myObject reference is not null, your myObject will not be garbage collected.

There are very few places you will likely need to use GC.Collect - I use it when creating add-ins for other applications however, as you are residing in their process.

Objects that are declared in logical scope usually only have the one reference on the stack, as you say.  When the stack pops, it will remove this one reference and be elligible for collection.

Dispose is a pattern implemented by .NET for deterministic release of unmanaged resources.  It has little to do with the Garbage Collector itself.  The GC does not call Dispose on your objects, it calls an object's finalizer - which in certain classes has been used to then call Dispose, as explained in my blog post.

Adam 
Huggy Bear replied to Naresh Kumar on 23-Mar-10 04:43 AM
Here is my answer

1. myObject.Dispose()
Your custom classes will not expose a Dispose method by default. You have to inherit your class from the IDisposable interface and implement its Dispose method. This is going to be your implementation so this will execute whatever code you have in the Dispose method of your class. Most developers implement this method to dispose the resources used by this class and also a GC.Collect() would be done.

2. GC.Collect()
It invokes the garbage collection to clean up the null referenced objects in memory. But it is not guaranteed that the Garbage collector would run mandatorily as soon as you say GC.Collect(). It will always do it in a periodic manner.

Hope this clarifies your doubts!
Naresh Kumar replied to Adam Houldsworth on 23-Mar-10 05:50 AM
Thanks for your reply.

Please clarify my doubts in this scenario,
private Void MyMethod1()
{
     MyMethod2();
     System.GC.Collect();
}
private Void MyMethod2()
{
       MyClass myObj = new MyClass();
       myObj.property = "some text";
       some code..
}

Here in the first method MyMethod1 I am calling another method creating an object and returning the control back to MyMethod1,
then I am calling System.GC.Collect();
Object will be removed from the heap when CLR runs the GarbageCollector on Heap or by the code System.GC.Collect(); written after calling the method MyMethod2(); ?

I can use System.GC.Collect() in the code this will not effect the CLR running GarbageCollector at runtime?

Please let me know the things how it goes.

Thanks in advance.

Adam Houldsworth replied to Naresh Kumar on 23-Mar-10 08:13 AM
Hi,

In this scenario, your MyClass instance will be popped from the stack and elligible for collection.

Calling GC.Collect, according to MSDN, forces an immediate collection of all generations:
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

So, in theory, your MyClass instance should be collected.

Adam
Adam Houldsworth replied to Naresh Kumar on 23-Mar-10 08:15 AM
Calling GC.Collect does not affect the automatic collection of objects - however, generally you don't need to do it as the GC is highly optimized.

Adam