明确删除 DOM 元素以避免 javascript 中的内存泄漏

Remove DOM element explicitly to avoid memory leakage in javascript

我正在查看 MDN 中关于垃圾回收的 doc。当我遇到 'Real-life example' 部分时,下面语句中的术语 'explicitly removed' 使我感到困惑。

If the property is not explicitly removed or nulled, a reference-counting garbage collector will always have at least one reference intact and will keep the DOM element in memory even if it was removed from the DOM tree

document.removeChild 能否满足明确删除 DOM 元素的条件?

Can document.removeChild fulfill the criteria of removing DOM element explicitly?

问题表明有些混乱:没有明确删除 DOM 节点的条件。从 DOM 中删除一个元素,无论是显式还是隐式(通过从它的下降中删除一个祖先元素),应该使其可用于内存收集,假设代码中没有保留对删除元素的引用。引用文章开头提到的属性是指示例中使用的div.circularReference属性,其值设置为div 创建从 属性 到元素的循环引用。

可以通过删除或更改 属性 的值来明确删除“属性”,例如

 div.circularReference = null; // or
 delete div.circularReference;

可以在从 DOM 中删除 div 之前或之后删除循环引用。好消息是文章说现代浏览器不再使用引用计数垃圾收集器。