What is the difference between Finalize and Dispose (Garbage collection)

Answer Posted / kirti

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object. In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.

Dispose can be called even if other references to the object are alive. Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from

permanently leaking if the programmer fails to call Dispose.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which is the root namespace for fundamental types in .net framework?

563


What are the difference between asynchronous controller implementation between asp.net mvc 3 & asp.net mvc 4?

583


Is entity framework slow?

524


what is entityclient?

684


How to return the JSON from action method in ASP.Net MVC?

691






Explain Bundle.Config in ASP.Net MVC4?

567


What is entitytype? : Entity framework

530


Where is the new functionality in the .net framework 3.0 (such as wcf, wf, wpf, and cardspace) installed to? Is that different from where the .net framework 2.0 is installed to?

544


What is meant by viewdata?

552


What is the use .glimpse in mvc?

597


What are actions in mvc?

553


How does the 'page lifecycle' of asp.net mvc works?

570


What are the main features of asp.net mvc 4 used by asp.net web api?

549


Where are the routing rules defined in an asp.net mvc application?

542


Can you explain the page life cycle of mvc?

555