Replies: 4 comments 5 replies
-
Because you didn't tickle the garbage collector the right way. |
Beta Was this translation helpful? Give feedback.
-
Are you going to suggest a modification to make it work? |
Beta Was this translation helpful? Give feedback.
-
I have a new example, an explanation and more questions Example let created = 0;
let destroyed = 0;
const registry = new FinalizationRegistry((heldValue) => {
++destroyed;
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
for (let i = 0; i < 1000; ++i) {
registry.register(new Array(100000), i);
++created;
console.log(`Destroyed: ${destroyed} Alive: ${created - destroyed}`);
await sleep(0); // give space to the finalisers
}
}
main(); This time the finalisers are called: one can check if the number of Explanation Since node is single threaded, the main thread must yield for the finalisers to run. Not surprisingly, all the working examples one can find online involve Questions
|
Beta Was this translation helpful? Give feedback.
-
It is actually a very common issue, which I think deserves at least a good example with the current limitations. |
Beta Was this translation helpful? Give feedback.
-
Why the following script only runs finalizers after the last line?
I have read all the disclaimers about the non deterministic nature of
FinalizationRegistry
andgc()
, but from this little experience it looks super deterministic, just not the way I wished.I tried to change number of iterations and array size to put more pressure, to no avail.
I run it as
node --expose-gc --trace-gc example.js
node --expose-gc example.js
node example.js
using
v18.13.0
on Ubuntu 23.04.Clearly this is toy example, but when I look at the behaviour of https://nodejs.org/api/n-api.html#napi_create_external for a c++ wrapper I wrote, I get the exact same behaviour, so I think it is something deeper.
Beta Was this translation helpful? Give feedback.
All reactions