Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

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

Solution 3:

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.

Post a Comment for "How Do I Explicitly Release A Javascript Object From Within The Object?"