Why you should care
For several reasons detailed in references, using finalizers is not recommended:
- The finalization is not determined. You don’t know when the finalizer will be called
- Due to the fact that an object with the finalizer does not get removed by the garbage collector immediately, the object, and the entire graph of dependent objects, go through the garbage collection and promote to the next generation. They will be removed only when the garbage collector decides to collect objects of this generation, which can take quite a while
- Since the finalizers run in a separate thread in parallel with other threads of the application, a programmer may have a situation when the new objects, requiring finalization, will be created faster than the finalizers of old objects will complete the execution. This will lead to increased memory consumption, decreased performance, and perhaps eventually to the crash of the application with OutOfMemoryException
- A finalizer may be not executed at all. Upon the abortion of the application, for example, if there is an exception thrown in somebody’s finalizer due to any of the reasons described above, no other finalizers will be executed. If you free unmanaged objects of the operating system, there will be nothing wrong in the way that the operating system returns its resources when the application terminates. But if you put unwritten bytes to the file, you will lose your data
How we detect
CAST Highlight counts one occurrence each time a finalizer is detected. A finalizer is detected by the presence of “~” preceding a method name that is also the name of a class.
class Nested { public void DoSomeWork() { Console.WriteLine(String.Format( "Thread {0} enters DoSomeWork", Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(2000); Console.WriteLine(String.Format( "Thread {0} leaves DoSomeWork", Thread.CurrentThread.ManagedThreadId)); } ~Nested() { Console.WriteLine("Finalization of Nested"); DoSomeWork(); } }
References
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.