process.finalization.register(ref, callback)
ref<Object> | <Function> 正在跟踪的资源的引用。callback<Function> 资源完成时要调用的回调函数。ref<Object> | <Function> 正在跟踪的资源的引用。event<string> 触发最终化的事件。默认为 'exit'。
此函数会注册一个回调,当进程触发 exit 事件且 ref 对象未被垃圾回收时调用该回调。如果对象 ref 在 exit 事件触发之前已被垃圾回收,该回调将从终结注册表中移除,并且在进程退出时不会被调用。
【This function registers a callback to be called when the process emits the exit
event if the ref object was not garbage collected. If the object ref was garbage collected
before the exit event is emitted, the callback will be removed from the finalization registry,
and it will not be called on process exit.】
在回调函数内部,你可以释放由 ref 对象分配的资源。请注意,所有适用于 beforeExit 事件的限制也同样适用于回调函数,这意味着在特殊情况下回调函数可能不会被调用。
【Inside the callback you can release the resources allocated by the ref object.
Be aware that all limitations applied to the beforeExit event are also applied to the callback function,
this means that there is a possibility that the callback will not be called under special circumstances.】
此功能的理念是帮助你在启动进程退出时释放资源,同时如果该对象不再使用,也允许其被垃圾回收。
【The idea of this function is to help you free up resources when the starts process exiting, but also let the object be garbage collected if it is no longer being used.】
例如:你可以注册一个包含缓冲区的对象,希望在进程退出时确保该缓冲区被释放,但如果对象在进程退出之前被垃圾回收,我们就不再需要释放该缓冲区,所以在这种情况下,我们只需从终结注册表中移除回调。
【Eg: you can register an object that contains a buffer, you want to make sure that buffer is released when the process exit, but if the object is garbage collected before the process exit, we no longer need to release the buffer, so in this case we just remove the callback from the finalization registry.】
const { finalization } = require('node:process');
// Please make sure that the function passed to finalization.register()
// does not create a closure around unnecessary objects.
function onFinalize(obj, event) {
// You can do whatever you want with the object
obj.dispose();
}
function setup() {
// This object can be safely garbage collected,
// and the resulting shutdown function will not be called.
// There are no leaks.
const myDisposableObject = {
dispose() {
// Free your resources synchronously
},
};
finalization.register(myDisposableObject, onFinalize);
}
setup();import { finalization } from 'node:process';
// Please make sure that the function passed to finalization.register()
// does not create a closure around unnecessary objects.
function onFinalize(obj, event) {
// You can do whatever you want with the object
obj.dispose();
}
function setup() {
// This object can be safely garbage collected,
// and the resulting shutdown function will not be called.
// There are no leaks.
const myDisposableObject = {
dispose() {
// Free your resources synchronously
},
};
finalization.register(myDisposableObject, onFinalize);
}
setup();上面的代码依赖于以下假设:
【The code above relies on the following assumptions:】
- 避免使用箭头函数
- 建议将常规函数放在全局上下文(根)中
常规函数可能会引用 obj 所在的上下文,从而导致 obj 无法被垃圾回收。
【Regular functions could reference the context where the obj lives, making the obj not garbage collectible.】
箭头函数将保留之前的上下文。例如,考虑以下情况:
【Arrow functions will hold the previous context. Consider, for example:】
class Test {
constructor() {
finalization.register(this, (ref) => ref.dispose());
// Even something like this is highly discouraged
// finalization.register(this, () => this.dispose());
}
dispose() {}
} 这个对象被垃圾回收的可能性很小(不是不可能),但如果没有被回收,dispose 会在调用 process.exit 时被执行。
【It is very unlikely (not impossible) that this object will be garbage collected,
but if it is not, dispose will be called when process.exit is called.】
请小心,并避免依赖此功能来处理关键资源,因为不能保证回调在所有情况下都会被调用。
【Be careful and avoid relying on this feature for the disposal of critical resources, as it is not guaranteed that the callback will be called under all circumstances.】