Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good samples of using Finalizers in C#

When I read a few articles about memory management in C#, I was confused by Finalizer methods.

There are so many complicated rules which related with them. For instance, nobody knows when the finalizers will be called, they called even if code in ctor throws, CLR doesn't guarantee that all finalizers be called when programs shutdowt, etc.

For what finalizers can be used in real life?

The only one example which I found was program which beeps when GC starts.

Do you use Finalizers in your code and may have some good samples ?

UPD:

Finalizers can be used when developpers want to make sure that some class always disposed correctly through IDisposable. (link ; Thanks Steve Townsend)

like image 828
mt_serg Avatar asked Nov 03 '10 15:11

mt_serg


People also ask

What are Finalizers in C #? Explain SuppressFinalize () method?

A finalizer, which is represented by the Object. Finalize method, is used to release unmanaged resources before an object is garbage-collected. If obj does not have a finalizer or the GC has already signaled the finalizer thread to run the finalizer, the call to the SuppressFinalize method has no effect.

Can we call Finalize method in C#?

The C# compiler does not allow you to override the Finalize method. Instead, you provide a finalizer by implementing a destructor for your class. A C# destructor automatically calls the destructor of its base class. Visual C++ also provides its own syntax for implementing the Finalize method.

Why use Dispose method in C#?

The dispose pattern is used for objects that implement the IDisposable interface, and is common when interacting with file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is unable to reclaim unmanaged objects.

When designing a class it is recommended to avoid using the finalize () method?

It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.” So, in one way we can not guarantee the execution and in another way we the system in danger. Better, don't use this method.


2 Answers

There is an exhaustive discussion of Finalizer usage, with examples, here. Link courtesy of @SLaks at a related answer.

See also here for a more concise summary of when you need one (which is "not very often").

There's a nice prior answer here with another good real-world example.

To summarize with a pertinent extract:

Finalizers are needed to guarantee the release of scarce resources back into the operating system like file handles, sockets, kernel objects, etc.

For more correct real-world examples, browse around affected classes in the .Net Framework this MSDN search:

http://social.msdn.microsoft.com/Search/en-US?query=%22.Finalize%22&ac=8

One valid reason I can think of when you might need to use a finalizer is if you wrap a third-party native code API in a managed wrapper, and the underlying native code API library requires the timely release of used operating system resources.

like image 75
Steve Townsend Avatar answered Sep 20 '22 11:09

Steve Townsend


The best practice known to me is plain simple don't use them. There might however be some corner cases when you want to use a finalizer, particularly when dealing with unmanaged objects and you can't implement Dispose pattern (I do not know legacy issues) then you can implement Finalize method with caution (and it could reduce the performance of your system, make your objects undead and other possibly weird scenarios, minding the exceptions as they are uncatchable:)).

In 99% of cases just write the use Dispose pattern and use this method to clean after yourself and everything will be fine.

like image 20
luckyluke Avatar answered Sep 23 '22 11:09

luckyluke