0

Possible Duplicate:
Finalize vs Dispose

Dispose - This will free up the object memory and GC should be supperessed in this case.

Finalize - In case the object is not disposed and when then object goes out of the scope(I mean when the class goes out of the scope) GC will say Finalize to clean it up.

Destructor - Don't know. Can you explain difference b/w destructors and finalize ?

Community
  • 1
  • 1
Pankaj
  • 9,749
  • 32
  • 139
  • 283

4 Answers4

2

Dispose cannot free up memory. The Dispose() method releases or closes the unmanaged resources.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

The destructor implicitly calls finalize, so it is sort of a pre-finalize.

See MSDN for more details. One important tidbit from that documentation:

Even with this explicit control over resources, the destructor becomes a safeguard to clean up resources if the call to the Dispose method failed.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
1

Descrtuctor is in c++ and Finalizers are in .NET. Althought the way your represent a finalizer in C# code looks like a C++ descructor, but it's not the same and its behavior is different too.

Finalization is the last process that happens in .NET memory management. Disposing is the pattern one cleans up unmanaged memory. Remember that Dispose is the operation carried out manually or explicitly called basis, whereas finalizer is not. It's automatic by the run time.

You may wish to read this

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
Zenwalker
  • 1,883
  • 1
  • 14
  • 27
  • should i suppress the finalize in destructors by gc.suppressfinalize as both finalize and destructors seems to clean up memory ? – Pankaj Jan 06 '12 at 04:19
  • If your following standard Dispose pattern suggested by MSDN, then one should suppressfinalize as Finalization process is very costly and doing it twice is of waste and some times problematic. So if your cleaning up memory on your own, then why GC has to take one more round for doing same? – Zenwalker Jan 06 '12 at 04:23
1

Finalize
It is used by the Garbage Collector implicitly to free the space.

Destructor
It is used to destroy the variable's value.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
4b0
  • 21,981
  • 30
  • 95
  • 142
  • see more for[http://stackoverflow.com/questions/456213/destructor-vs-idisposable] and [http://stackoverflow.com/questions/8753205/dispose-finalize-and-destructors/8753255#8753255] – 4b0 Jan 06 '12 at 04:16