如何从对象内部显式释放 JavaScript 对象?
我正在使用 John Resig 的配方来实现 JavaScript“类”和继承。对于这个问题,我已将代码剥离回类似的内容:
MyClass = Class.extend({
// create an <h3>Hello world!</h3> in the HTML document
init : function (divId) {
this._divId = divId;
this._textDiv = document.createElement("h3");
this._textDiv.innerHTML = "Hello world!";
document.getElementById(divId).appendChild(this._textDiv);
},
// remove the <h3> and delete this object
remove : function () {
var container = document.getElementById(this._divId);
container.parentNode.removeChild(container);
// can I put some code here to release this object?
}
});
一切正常:
var widget = new MyClass("theDivId");
...
widget.remove();
我将在一个页面上放置数百个这样的东西(显然具有一些合理的功能),并且我想要一种简单的方法来发布每个对象的内存。我知道我可以使用 widget = null;
并相信 GC 在需要时释放对象(?),但是我可以在 remove()
方法中执行一些明确的操作吗?我知道将 this = null;
放在 remove()
末尾是行不通的;)
I'm using John Resig's recipe for JavaScript 'classes' and inheritance. I've stripped my code back to something like this for this question:
MyClass = Class.extend({
// create an <h3>Hello world!</h3> in the HTML document
init : function (divId) {
this._divId = divId;
this._textDiv = document.createElement("h3");
this._textDiv.innerHTML = "Hello world!";
document.getElementById(divId).appendChild(this._textDiv);
},
// remove the <h3> and delete this object
remove : function () {
var container = document.getElementById(this._divId);
container.parentNode.removeChild(container);
// can I put some code here to release this object?
}
});
All works well:
var widget = new MyClass("theDivId");
...
widget.remove();
I'm going to have hundreds of these things on a page (obviously with some sensible functionality) and I'd like a simple way to release the memory for each object. I understand I can use widget = null;
and trust the GC releases the object when required (?), but can I do something explicit in the remove()
method? I know that placing this = null;
at the end of remove()
doesn't work ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可以。您没有任何方法可以直接访问垃圾收集器。正如您所说,您能做的最好的事情就是确保该对象不再被引用。
IMO,这样更好。垃圾收集器比你(和我)聪明得多,因为编写这个东西已经经过多年的研究,即使你尝试进行优化,你可能仍然没有做得比它更好。
当然,如果您与 JS 引擎交互,您将能够控制执行并强制垃圾收集(等等),尽管我非常怀疑您处于那个位置。如果您有兴趣,请下载并编译 Spider Monkey(或 v8,或任何您喜欢的引擎),并且在 repl 中我认为它的
gc()
两者都适用。这让我想到了另一点,因为该标准没有定义垃圾收集的内部结构,即使您设法确定在代码中的某个时刻调用 gc 是有帮助的,但很可能不会获得相同的好处所有平台。
No. You don't have any way of accessing the garbage collector directly. As you say, the best you can do is make sure the object is no longer referenced.
IMO, it's better that way. The garbage collector is much smarter than you (and me) because years of research has gone into writing the thing, and even when you try and make optimisations, you're likely still not doing a better job than it would.
Of course if you're interfacing with a JS engine you will be able to control the execution and force garbage collection (among much more), although I very much doubt you're in that position. If you're interested, download and compile spider monkey (or v8, or whatever engine tickles your fancy), and in the repl I think its
gc()
for both.That brings me to another point, since the standard doesn't define the internals of garbage collection, even if you manage to determine that invoking the gc at some point in your code is helpful, it's likely that that will not reap the same benefits across all platforms.
没有办法手动销毁对象,唯一的方法是释放对象的所有链接并信任删除到 GC
实际上,在您的代码中,您也应该清除remove方法中的
this._textDiv = null
和container = null
,因为在某些浏览器中这可能是 GC 的问题there is no ways to destroy objects manually, only way is to free all links to your object and trust removal to GC
actually in your code you should clear
this._textDiv = null
andcontainer = null
in remove method too, because it can be a problem for GC in some browsersthis
是一个关键字,您不能为其分配任何值。从作用域中删除对象的唯一方法是手动将null
分配给每个变量。然而,这种方法并不总是有效:在 XMLHttpRequest 的某些实现中,在从内存中释放 XMLHttpRequest 对象之前,必须重置
onreadystate
和open
函数。this
is a keyword, to which you cannot assign any value. The only way to remove objects from a scope is to manually assignnull
to every variable.This method doesn't always work, however: In some implementations of the XMLHttpRequest, one has to reset the
onreadystate
andopen
functions, before the XMLHttpRequest object is freed from the memory.