views:

151

answers:

6

Hi all,

I am little bit confused about use of destructor in c#. In my knowledge we can't call destructor acording to my wish it will call automatically before garbage collector for performing some work over class (object) so i want to ask if we are using destructor in c# then whe we need to garbage collector. As i know that destructor can take care of memory then why we need to garbage collector ?

+2  A: 

The destructor is not for cleaning up managed memory. That is what the garbage collector is for. The destructor is for cleaning up other resources such as handles.

I recommend that you take a look at CLR via C# for details on how this works.

Brian Rasmussen
+1  A: 

Garbage collection takes care of most normal object creation and disposal scenarios for you -- you could do the explicit memory allocation/deallocation, but GC frees you from having to do that.

However, if you use unmanaged resources (such as file handles, database connections, etc.), you will want to explicitly release them when you're done. That's the kind of stuff you'd typically put in your destructors.

Anna Lear
Why the downvote?
Anna Lear
Don't put it in a destructor, put it in Dispose() and implement IDisposable. Destructors are never guaranteed to run, so your resources are never guaranteed to be cleaned up (at least until program exit).
siride
Fair enough. Still, I think my answer addresses the original question of "what's the point of GC if we can do it all ourselves".
Anna Lear
+3  A: 

I think based on reading your almost duplicate topic that you don't have a well understanding of how the Garbage Collector works. In a very blunt and brief manner, it is its own service that runs in the background, it tracks and frees memory for unused and disposed objects throughout the entire lifetime of your application. Realistically, you should never have to call the GC yourself, unless in very rare and specific cases.

The desctructors are used to clean up and free unmanaged resources that cannot be freed by the Garbage Collector, see this MSDN page for more information on destructors.

David Anderson
+1 for speaking out loud that the best advice is probably to brush up his knowledge about memory management, garbage collection and native resources/dispose in .NET
Jim Brissom
+3  A: 

It would also be useful to read about the correct way to implement IDisposable pattern. There is much more to it than what we think -

http://msdn.microsoft.com/en-us/magazine/cc163392.aspx

Unmesh Kondolikar
+7  A: 

Everybody thinks about garbage collection the wrong way:

A correctly-written program cannot assume that finalizers will ever run.

Remus Rusanu
+1 Pretty much the best answer so far, and the only one that's actually correct. At best, you put a call to Dipose() in your finalizer, in case Dispose() wasn't called manually by other code in your program.
siride
Great words. Have been added the article to bookmarks
zerkms
Also the article linked by Unmesh is a very god resource, http://msdn.microsoft.com/en-us/magazine/cc163392.aspx. There are many other articles on web that mention the `Dispose(bool disposing)` pattern w/o actually explaining the difference between 'native' and 'managed' resources. I've seen plenty a classes that were needlessly implementing the pattern to dispose *managed* resources.
Remus Rusanu
Ahhh... here I was thinking about Garbage Collection as collection of garbage when I should've been thinking about it as putting out fires. Joking aside, I'm not convinced the article really helps, its better to think of closing streams and files as a business logic requirement than a resource requirement (which ultimately is up to the OS in any case)
CurtainDog
The closing streams and files duty in C# belongs to the `IDisposable` and `using` pattern, not to finalizers and destructors.
Remus Rusanu
+1  A: 

I think the confusion here comes from the fact that you can dispose of objects both deterministically and non-deterministically (i.e. when the GC gets round to doing it).

To answer your questions as to why we need a GC at all I would say, even putting aside memory leaks, that GCs are quite performant, and a having a requirement of immediately reclaiming memory might actually lower the total performance of the system. Its a similar argument to the single- vs multi- threaded debate.

CurtainDog