如何从对象内部显式释放 JavaScript 对象?

发布于 2024-12-04 17:10:42 字数 1107 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-12-11 17:10:42

不可以。您没有任何方法可以直接访问垃圾收集器。正如您所说,您能做的最好的事情就是确保该对象不再被引用。

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.

与风相奔跑 2024-12-11 17:10:42

没有办法手动销毁对象,唯一的方法是释放对象的所有链接并信任删除到 GC
实际上,在您的代码中,您也应该清除remove方法中的 this._textDiv = nullcontainer = 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 and container = null in remove method too, because it can be a problem for GC in some browsers

夜唯美灬不弃 2024-12-11 17:10:42

this 是一个关键字,您不能为其分配任何值。从作用域中删除对象的唯一方法是手动将 null 分配给每个变量。

然而,这种方法并不总是有效:在 XMLHttpRequest 的某些实现中,在从内存中释放 XMLHttpRequest 对象之前,必须重置 onreadystateopen 函数。

this is a keyword, to which you cannot assign any value. The only way to remove objects from a scope is to manually assign nullto every variable.

This method doesn't always work, however: In some implementations of the XMLHttpRequest, one has to reset the onreadystate and open functions, before the XMLHttpRequest object is freed from the memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文