I am building a JavaScript game that creates a Level
object using var:
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:
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?
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()
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.
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.
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.
Update 2: The proposal has been implemented in various browsers as WeakRef
Normally in JavaScript garbage collection is non deterministic. You cant know if or when an object is garbage collected. This applies to objects that are strong referenced.
In ES12 and after, you can use a FinalizationRegistry
.
Finalizers lets you handle when an object is garbage collected, using a JavaScript callback. Still the limitation is that, when the callback will get executed is non deterministic. It may take a min or an hour.
// object creation
let abc = new Array(200).fill(true);
const cleanup = new FinalizationRegistry(key => {
// your code here
});
// tagging variable abc to finalizer
cleanup.register(abc, 'werwer');
// abc = null;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With