简体   繁体   English

JavaScript对象:销毁它们

[英]JavaScript objects: Destroying them

I have to maintain a JavaScript object with some 30-40 properties, which I update every few seconds. 我必须维护一个包含30-40个属性的JavaScript对象,我每隔几秒就会更新一次。 I have read that there is no such thing as "freeing" memory in JavaScript, and the browser automatically garbage collects unused memory. 我已经读过在JavaScript中没有“释放”内存,浏览器会自动垃圾收集未使用的内存。

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? 我的问题是:是否足以将对象本身设置为null,或者我是否需要将其所有属性设置为null然后将其设置为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; 

Variables are not garbage collected until there is at least one reference to them. 在至少有一个对它们的引用之前,变量不会被垃圾收集。 However, be aware that global variables are visible from "everywhere" and may sometimes not get garbage collected, because they are still visible from somewhere. 但是,请注意全局变量在“无处不在”中是可见的,有时可能无法收集垃圾,因为它们仍然可以从某个地方看到。

For instance, you have 例如,你有

var a = {"testkey": "testval"};

var b = jQuery.ajax({
   url:"http://somewhere",
   method: "GET",
   onSuccess: function() {
      //this function is called asynchronously, moments later,
      //but as "a" is defined in the enclosing variable scope,
      //you can access it from here
      alert(a.testkey);
   }
});

Therefore, I would consent to setting value to null after you are done with your object. 因此,在完成对象后,我同意将值设置为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. 您可以通过将引用该对象的所有局部变量设置为null (任何其他值也可以)以及删除(或覆盖)引用它的其他对象的任何属性来实现此目的。

Deleting the object's own properties or explicitly setting them to null won't gain you anything in this regard: The references of the object won't be considered 'alive' if itself isn't. 删除对象自己的属性或将它们显式设置为null将不会在这方面获得任何东西:如果对象的引用本身不是,则不会将其视为“活动”。

The only times (I can think of) where you would need to set items to null are a few cases where the DOM is involved. 我需要将项目设置为null的唯一时间(我能想到)是涉及DOM的几种情况。

For example, if you have a node with several child nodes each with an onclick handler defined by anonymous functions, then setting each onclick to null would be a good idea (to avoid unintentional circular references). 例如,如果您有一个带有多个子节点的节点,每个子节点都有一个由匿名函数定义的onclick处理程序,那么将每个onclick设置为null将是一个好主意(避免无意的循环引用)。

<div id="delete_me">
  <span></span>
  <span></span>
</div>

var theDiv = document.getElementById('delete_me');
for (var i=0; i < theDiv.childNodes.length; i++) {
  theDiv.childNodes[i].onclick = function () {
    // stuff
  };
}

// Later...
// Delete stuff.
var divToDelete = document.getElementById('delete_me');

// Remove all the onclicks
for (var i=0; i < theDiv.childNodes.length; i++) {
  theDiv.childNodes[i].onclick = null;
}

// Delete the div
divToDelete.parentNode.removeChild(divToDelete);

Setting properties to null is unnecessary. 将属性设置为null是不必要的。 Any values stored in properties will be freed by the garbage collector. 存储在属性中的任何值都将被垃圾回收器释放。 As an alternative, the delete operator will likely force immediate destruction: 作为替代方案, 删除操作符可能会强制立即销毁:

delete obj;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM