views:

136

answers:

6

If my class has NO destructor, an it goes out of scope. GC runs at certain time, now will it simply reclaim memory from my class OR will it call its destructor or Finalize () on it ?

And does the .net framework class like SQLConnection implement a destructor? I saw it has a Dispose () implementation but didn't see the destructor using "Go to definition".

A: 

The default implentation of Finalize() does nothing as far as I know, so if it calls it or not shouldn't matter.

The vast majority of classes don't need a custom finalizer, if you are making use of managed resources the Garbage Collector will be able to clean it up.

If your class doesn't make use of unmanaged resources then you should avoid the use of a Finalize implentation as they take time.

AndyC
thanks I am aware of these and have read thru many articles, thought someone will know the exact answer to this, so posted here.
isthatacode
+2  A: 

Have a read through this article it may help but from what i know if there is a destructor / Finalise it will be called else GC will siply free up the memory.

http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

MarcoF
I have read this one, if there is no destructor, then GC will not call a destructor. But will it call Finalize on it ? Is there some way to check it?
isthatacode
@isthatacode if you implement a destructor it will be called. If not, the memory is just freed.
AndyC
@isthatacode: The destructor is just a special syntax used to override `Finalize`.
LukeH
@AndyC is the same applicable for Finalize() ?
isthatacode
@isthatacode A destructor is a finalize method. The ~Class() syntax is what is used to override Finalize().
AndyC
@AndyC - Yes its right, This is what i found on msdn, destructor code is implicitly translated to the Finalize method: class Car { ~Car() // destructor { // cleanup statements... } } protected override void Finalize() { try { // Cleanup statements... } finally { base.Finalize(); } }
isthatacode
+2  A: 

If an object has no destructor then it isn't added to the finalisation queue in the first place, so the GC will simply release memory etc when it cleans up.

The GC only calls the destructor/finalisation code for objects in the finalisation queue.

LukeH
@Luke - thanks this makes sense.
isthatacode
A: 

This is pitiful. Shame on all the people that "answered" this question while knowing nothing, nor actually answering the question.

Here is your answer:

This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

From:

http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx

gmagana
@gmagana: Your own answer is misleading: The `Finalize` method is only called for objects that were added to the finalisation queue (that is, those objects that override the `Finalize` method).
LukeH
Yea, shame on people for trying to help.
AndyC
@gmagana- thanks for ur reply . I think we shud not use insulting language here.
isthatacode
@Luke: LOL, if my answer is misleading, then the docs are misleading. I cut and pasted from the docs.
gmagana
+2  A: 

Finalize is called on the object only when the object is placed on the finalization queue. A way to put it there is to give it a destructor. In C# if there is no destructor the object will not be placed on the finalization queue.

As others pointed out do not use destructors unless you must as it is slowing down garbage collection. When GC finds out that such an object can be reclaimed, instead of freeing memory it places the object on the finalization queue. In other words the object survives initial GC and will only be reclaimed after finalizers will be executed

mfeingold
A: 

You can't call a destructor in C#, per se. They are invoked automatically.

You can instantiate a new object, and so long as all of its resources are managed (read: do not implement IDisposible), it will be garbage collected when:

  • The Garbage collector decides to run
  • No references remain that point to that object

For classes that implement IDisposible, your best bet is to wrap their usage in a using statement, which will ensure that the Dispose() method is called when that object goes out of scope.

Suggested Reading:

George Stocker