最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I explicitly release a JavaScript object from within the object? - Stack Overflow

programmeradmin0浏览0评论

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 ;)

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 ;)

Share Improve this question asked Sep 15, 2011 at 20:35 wpearsewpearse 2,4222 gold badges30 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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 pile 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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论