wpearse
wpearse

Reputation: 2422

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

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

Upvotes: 3

Views: 4724

Answers (3)

davin
davin

Reputation: 45555

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.

Upvotes: 2

Rob W
Rob W

Reputation: 349142

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.

Upvotes: 1

Dmitry
Dmitry

Reputation: 902

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

Upvotes: 2

Related Questions