--- title: FinalizationRegistry slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry tags: - Class - FinalizationRegistry - JavaScript - Reference translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry ---
FinalizationRegistry
オブジェクトにより、オブジェクトがガベージコレクションで回収されるときにコールバックを要求することができます。
FinalizationRegistry
は、レジストリに登録されているオブジェクトが回収される (ガベージコレクションされる) 時にクリーンアップコールバックを要求する方法を提供します。(クリーンアップコールバックはファイナライザーと呼ばれることもあります。)
注: Cleanup callbacks should not be used for essential program logic. 詳しくは Notes on cleanup callbacks を見てください。
コールバックで渡すレジストリを作成します。
const registry = new FinalizationRegistry(heldValue => { // .... });
次に、 register
メソッドを呼び出して、クリーンアップコールバックを行いたいオブジェクトを登録し、そのオブジェクトと保持値を渡します。
registry.register(theObject, "some value");
レジストリがオブジェクトへの強い参照を保持すると、目的に反してしまうので、 (レジストリが強い参照を保持していれば、そのオブジェクトは決して回収されない)、強い参照は保持はしません。
theObject
が回収された場合、クリーンアップコールバックは、指定した保持値 (上では "some value"
) で呼び出される可能性があります。保持値は、プリミティブでもオブジェクトでも、 undefined
であっても構いません。保持値がオブジェクトの場合、レジストリはその値への強い参照を保持します (これにより、後でクリーンアップコールバックに渡すことができます)。
オブジェクトの登録を解除したい場合は、三番目の値を渡します。 これは、後でレジストリのレジストリの unregister
関数をコールしてオブジェクトの登録を解除する際に使用する登録解除トークンです。レジストリは、登録解除トークンへの弱い参照のみを保持します。
よくオブジェクト自身が登録解除トークンとして使われ、これは良い結果になります。
registry.register(theObject, "some value", theObject); // ...some time later, if you don't care about `theObject` anymore... registry.unregister(theObject);
ただし、同じオブジェクトである必要はありません。異なるものでも構いません。
registry.register(theObject, "some value", tokenObject); // ...some time later, if you don't care about `theObject` anymore... registry.unregister(tokenObject);
FinalizationRegistry
オブジェクト生成します。Correct use of FinalizationRegistry
takes careful thought, and it's best avoided if possible. It's also important to avoid relying on any specific behaviors not guaranteed by the specification. When, how, and whether garbage collection occurs is down to the implementation of any given JavaScript engine. Any behavior you observe in one engine may be different in another engine, in another version of the same engine, or even in a slightly different situation with the same version of the same engine. Garbage collection is a hard problem that JavaScript engine implementers are constantly refining and improving their solutions to.
Here are some specific points that the authors of the WeakRef proposal that FinalizationRegistry is part of included in its explainer document:
Garbage collectors are complicated. If an application or library depends on GC cleaning up a FinalizationRegistry or calling a finalizer [cleanup callback] in a timely, predictable manner, it's likely to be disappointed: the cleanup may happen much later than expected, or not at all. Sources of variability include:
- One object might be garbage-collected much sooner than another object, even if they become unreachable at the same time, e.g., due to generational collection.
- Garbage collection work can be split up over time using incremental and concurrent techniques.
- Various runtime heuristics can be used to balance memory usage, responsiveness.
- The JavaScript engine may hold references to things which look like they are unreachable (e.g., in closures, or inline caches).
- Different JavaScript engines may do these things differently, or the same engine may change its algorithms across versions.
- Complex factors may lead to objects being held alive for unexpected amounts of time, such as use with certain APIs.
Some notes on cleanup callbacks:
FinalizationRegistry
instance itself is no longer reachable by JavaScript code.You create the registry passing in the callback:
const registry = new FinalizationRegistry(heldValue => { // .... });
Then you register any objects you want a cleanup callback for by calling the `register` method, passing in the object and a *held value* for it:
registry.register(theObject, "some value");
仕様書 |
---|
{{SpecName('WeakRefs', '#sec-finalization-registry-objects', 'FinalizationRegistry')}} |
{{Compat("javascript.builtins.FinalizationRegistry")}}