如何检测对象是否已在Javascript中进行垃圾回收?

时间:2022-09-07 23:58:00

I am building a javascript game that creates a 'level' object using var:

我正在构建一个使用var创建'level'对象的javascript游戏:

function start() {
   var myGameLevel = new Level(2);
}

This "Level()" object has a lot of functionality, primarily adding elements to the DOM and making them interactive. A simplification:

这个“Level()”对象具有很多功能,主要是向DOM添加元素并使它们具有交互性。简化:

function Level(i) {
    var _difficulty = i;

    this.init = function(){
         jQuery("#container").append(...game elements here...);
         jQuery("#button").on('click', function() {...});
    }
}

My question: how can I know if the Level object created in the 'start' function has been garbage collected or not? I aim to use only "var" variables so that there are no external references. When the DOM is cleared of all game elements, I EXPECT the 'level' object to be released from memory, but how can I be sure?

我的问题:如何知道在'start'函数中创建的Level对象是否已被垃圾回收?我的目标是只使用“var”变量,以便没有外部引用。当DOM被清除所有游戏元素时,我预计“级别”对象将从内存中释放,但我怎么能确定?

2 个解决方案

#1


5  

Weak references are considered a security risk, and thus not available to unprivileged code in browsers.

弱引用被认为是安全风险,因此不适用于浏览器中的非特权代码。

Those concerns don't apply to privileged code or serverside javascript execution, e.g. via node.js, and thus platform-specific weak reference implementations may be available to them.
E.g. firefox addons can use Components.utils.getWeakReference()

这些问题不适用于特权代码或服务器端javascript执行,例如通过node.js,因此可以使用特定于平台的弱参考实现。例如。 firefox插件可以使用Components.utils.getWeakReference()

For certain programming patterns WeakMap/WeakSet may be sufficient, but they do not allow the program to observe garbage collection because to do so it would need a key to probe those data structures, but holding onto that key would prevent the objects from being collected in the first place.

对于某些编程模式,WeakMap / WeakSet可能就足够了,但它们不允许程序观察垃圾收集,因为这样做需要一个密钥来探测这些数据结构,但保留该密钥会阻止对象被收集到第一名。

An additional concern voiced by JS implementors is that depending on how powerful a hypothetical weak ref APIs would be - e.g. offering finalization notifications - it could expose considerable amounts of GC behavior which in turn could constrain future implementations because changing behavior might break web applications.

JS实现者提出的另一个问题是,取决于假设的弱参考API的强大程度 - 例如提供最终通知 - 它可能会暴露大量的GC行为,这反过来可能会限制未来的实现,因为改变行为可能会破坏Web应用程序。


Update: There now is a proposal to standardize weak references in JS that mitigates the perceived risks by tying the release of weakly reachable objects to the JS event loop, making the behavior more deterministic.

更新:现在有一个建议来标准化JS中的弱引用,通过将弱可达对象的释放绑定到JS事件循环来减轻感知风险,从而使行为更具确定性。

#2


3  

I dont think you can control JavaScript garbage collection. Normally an variable or object can be collected if there are no references to it. So you can increase your chances of having an object collected by designing your logic to make the object go out of scope.

我不认为你可以控制JavaScript垃圾收集。通常,如果没有对它的引用,则可以收集变量或对象。因此,您可以通过设计逻辑来增加获取对象的机会,从而使对象超出范围。

#1


5  

Weak references are considered a security risk, and thus not available to unprivileged code in browsers.

弱引用被认为是安全风险,因此不适用于浏览器中的非特权代码。

Those concerns don't apply to privileged code or serverside javascript execution, e.g. via node.js, and thus platform-specific weak reference implementations may be available to them.
E.g. firefox addons can use Components.utils.getWeakReference()

这些问题不适用于特权代码或服务器端javascript执行,例如通过node.js,因此可以使用特定于平台的弱参考实现。例如。 firefox插件可以使用Components.utils.getWeakReference()

For certain programming patterns WeakMap/WeakSet may be sufficient, but they do not allow the program to observe garbage collection because to do so it would need a key to probe those data structures, but holding onto that key would prevent the objects from being collected in the first place.

对于某些编程模式,WeakMap / WeakSet可能就足够了,但它们不允许程序观察垃圾收集,因为这样做需要一个密钥来探测这些数据结构,但保留该密钥会阻止对象被收集到第一名。

An additional concern voiced by JS implementors is that depending on how powerful a hypothetical weak ref APIs would be - e.g. offering finalization notifications - it could expose considerable amounts of GC behavior which in turn could constrain future implementations because changing behavior might break web applications.

JS实现者提出的另一个问题是,取决于假设的弱参考API的强大程度 - 例如提供最终通知 - 它可能会暴露大量的GC行为,这反过来可能会限制未来的实现,因为改变行为可能会破坏Web应用程序。


Update: There now is a proposal to standardize weak references in JS that mitigates the perceived risks by tying the release of weakly reachable objects to the JS event loop, making the behavior more deterministic.

更新:现在有一个建议来标准化JS中的弱引用,通过将弱可达对象的释放绑定到JS事件循环来减轻感知风险,从而使行为更具确定性。

#2


3  

I dont think you can control JavaScript garbage collection. Normally an variable or object can be collected if there are no references to it. So you can increase your chances of having an object collected by designing your logic to make the object go out of scope.

我不认为你可以控制JavaScript垃圾收集。通常,如果没有对它的引用,则可以收集变量或对象。因此,您可以通过设计逻辑来增加获取对象的机会,从而使对象超出范围。