I have to maintain a JavaScript object with some 30-40 properties, which I update every few seconds. I have read that there is no such thing as “freeing” memory in JavaScript, and the browser automatically garbage collects unused memory.
My question is: is it enough to set the object itself to null, or do I need to set all its properties to null and then set it to null?
var obj = [];
obj[prop1] = "123";
obj[prop2] = "456";
//...and so on...
// now to release the obj, is it enough if I just did:
obj = null;
An object can only be collected if it is no longer reachable. You can achieve this by setting all local variables which reference the object to
null(any other value would do as well) and deleting (or overwriting) any properties of other objects which reference it.Deleting the object’s own properties or explicitly setting them to
nullwon’t gain you anything in this regard: The references of the object won’t be considered ‘alive’ if itself isn’t.