Упрощенный демонстрационный случай:
Код: Выделить всё
// Only works in Node >= v14.6.0 with --expose-gc
if (typeof gc !== 'function') {
throw new Error('Run the test with --expose-gc flag');
}
let timeoutId;
const registry = new FinalizationRegistry(() => {
clearTimeout(timeoutId);
console.log("item was successfully garbage collected");
});
let element = {};
registry.register(element, 'item');
timeoutId = setTimeout(() => {
console.error("item was not garbage collected in 3 seconds");
}, 3000);
element = null;
console.log("reference to item removed");
Promise.resolve().then(() => {
console.log("trigger garbage collection...");
gc();
});
Я пробовал оба в Nodejs 24 и Chrome 138. Play Play Phoply Play Phoply Play Phoply Play Phoply Play Phoply Play Phoply Phoply Phoply. Я понимаю, что здесь идет не так? Я нашел документацию о функции gc () гораздо менее полной, чем другие функции Javascript. Использование завершающей регистрации и global.gc () будет жить только в тестах, а не в производственном коде.]
Подробнее здесь: https://stackoverflow.com/questions/797 ... ascript-fu
Мобильная версия