The Dispose method is used to tell the object to free any unmanaged
resources that it uses. Once this is done, the object only contains
managed resources, and can be handled by the garbage collector once the
reference to the object is removed.
The Dispose method only tells the object to free resources, it doesn't
remove the object itself. The object will be removed when the garbage
collector finds it convenient.
If you don't call Dispose before removing the reference to the object,
two things can happen when the object is to be collected:
:: If the object has a finalizer, the garbage collector will try to use
that to tell the object to free the resources. The object will be
collected at earliest by the next garbage collection.
:: If the object doesn't have a finalizer, it will be collected and the
unmanaged resources will not be freed.
Generally the garbage collector should be left alone. It will free
memory when it's needed, and forcing a collection is not a good way to
try to free unmanaged resources.
> I use webBrowser to open a Word document,
> when I use this.webBrowser.dispose(), the WinWord Process end.
> while I use this.webBrowser = null, GC.Collect() , the WinWord Process
> still alive.
> I want to ask what's the difference between these two function.
Hi George
calling a Dispose method on a object frees up the any resources held the
object. It can be called on the objects of classes which implemets
IDisposable interface.
Garbage Collector only fress memory if there are other applicatins which
require more memory. If there is plenty of memory available it will not free
memory.
Regards,
Nitin Mittal
> I use webBrowser to open a Word document,
> when I use this.webBrowser.dispose(), the WinWord Process end.
> while I use this.webBrowser = null, GC.Collect() , the WinWord Process
> still alive.
> I want to ask what's the difference between these two function.