Execution penalty for finalizer if it has not been called

I have a class with a finalizer. But since I always call Dispose() , and Dispose() calls GC.SupressFinalize(this) , I think my object never makes it in the finalization queue. The finalizer is just as a backstop if another class user forgets to call Dispose() .

Is there any performance limitation for simply executing the finalizer, even if it has never been called and the object never ends up in the finalization queue?

I hadn’t thought before, but on page 102 of Bill Wagner’s Effective C #: Second Edition, it says, “Even if he never called, the presence of the finalizer does result in a pretty big performance penalty for your types.”

+4
source share
2 answers

Is there any performance limitation for simply executing the finalizer, even if it has never been called and the object never ends up in the finalization queue?

As long as you do this correctly and call GC.SuppressFinalize on your object, a "penalty" only occurs when the user does not call Dispose() .

Thus, the “severe” punishment is actually not so strong in most cases. It would be a problem if you had many short objects with finalizers that pollute the garbage collection process, but this is rarely a problem, since objects with finalizers are rare (in general).

+2
source

IIRC, the problem with finalizers is that objects with them survive for an extra gc loop. This only applies if the finalizer is called. This article is well explained: http://anasoft.wordpress.com/2007/06/17/more-about-gc-dispose-and-finalize/

0
source

Source: https://habr.com/ru/post/1338196/


All Articles