process.finalization.register(ref, callback)
¥Stability: 1.1 - Active Development
-
ref
<Object> | <Function> 对正在跟踪的资源的引用。¥
ref
<Object> | <Function> The reference to the resource that is being tracked. -
callback
<Function> 资源最终确定后要调用的回调函数。¥
callback
<Function> The callback function to be called when the resource is finalized.-
ref
<Object> | <Function> 对正在跟踪的资源的引用。¥
ref
<Object> | <Function> The reference to the resource that is being tracked. -
event
<string> 触发最终确定的事件。默认为 'exit'。¥
event
<string> The event that triggered the finalization. Defaults to 'exit'.
-
此函数注册一个回调,如果 ref
对象未被垃圾收集,则在进程触发 exit
事件时调用该回调。如果在触发 exit
事件之前对象 ref
被垃圾收集,则回调将从终止注册表中删除,并且在进程退出时不会调用它。
¥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
事件的所有限制也适用于 callback
函数,这意味着在特殊情况下可能不会调用回调。
¥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:
-
避免使用箭头函数
¥arrow functions are avoided
-
建议将常规函数放在全局上下文(根)内
¥regular functions are recommended to be within the global context (root)
常规函数可以引用 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() {}
}
此对象不太可能(并非不可能)被垃圾收集,但如果没有,则在调用 process.exit
时将调用 dispose
。
¥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.