|
In object-oriented programming, a finalizer or finalize method is a special method that performs finalization, generally some form of cleanup. A finalizer is executed during object destruction, prior to the object being deallocated, and is complementary to an initializer, which is executed during object creation, following allocation. Finalizers are strongly discouraged by many, due to difficulty in proper use and the complexity they add, and alternatives are suggested instead, primarily the dispose pattern – see problems with finalizers. The term "finalizer" is primarily used in object-oriented languages that use garbage collection, of which the archetype is Java. This is contrasted with a "destructor", which is a method called for finalization in languages with deterministic object lifetimes, archetypically C++. These are generally exclusive – a language will have either finalizers (if garbage collected) or destructors (if deterministic), but in rare cases a language may have both, as in C++/CLI and D, and in case of reference counting (instead of tracing garbage collection), terminology varies. In technical usage, "finalizer" may also be used to refer to destructors, as these also perform finalization, and some subtler distinctions are drawn – see terminology. For this article, "finalizer" refers only to a method used for finalization in a garbage-collected language; for discussion of finalization generally, see finalization. ==Use== Finalizers are generally used to perform some form of cleanup, similar to destructors, but differ in that destructors are executed deterministically, while finalizers are executed at the discretion of the garbage collector, which is usually not deterministic. However, a finalizer can include arbitrary code; a particularly complex use is to automatically return the object to an object pool. Finalizers are generally both much less necessary and much less used than destructors. They are much less necessary because garbage collection automates memory management, and much less used because they are not generally executed deterministically – they may not be called in a timely manner, or even at all, and the execution environment cannot be predicted – and thus any cleanup that must be done in a deterministic way must instead be done by some other method, most frequently manually via the dispose pattern. Notably, both Java and Python do not guarantee that finalizers will ever be called, and thus they cannot be relied on for cleanup. Due to the lack of programmer control over their execution, it is usually recommended to avoid finalizers for any but the most trivial operations. In particular, operations often performed in destructors are not usually appropriate for finalizers. A common anti-pattern is to write finalizers as if they were destructors, which is both unnecessary and ineffectual, due to differences between finalizers and destructors. This is particularly common among C++ programmers, as destructors are heavily used in idiomatic C++, following the Resource Acquisition Is Initialization (RAII) idiom. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Finalizer」の詳細全文を読む スポンサード リンク
|