Tuesday, August 9, 2016

IDisposable vs finalize c#

Garbage collection
GC reclaims the memory used by the object when the object is referenced no more.

Finalize
  • Release managed resource.
  • Garbage Collector will call automatically, we do not have control to call.
  • It belongs to Object Class
  • Finalize is in Destructor, it will override the finalize method, and call automatically.
  • garbage collector invokes the method finalize() just before it destroys the object completely.
Dispose
  • Release managed and unmanaged resource immediately when someone called.
  • It belongs to IDisposable Interface
  • Use for files handler, DB connections.
  • Using Statement

Always Use Dispose, Use Finalize unless it really necessary, if not it will call automatically.

Managed resources  is managed by the garbage collector. When you no longer have any references to a managed object , the garbage collector will release that memory for you.
Unmanaged resources are then everything that the garbage collector does not know about. For example: files, data connections

No comments:

Post a Comment