I just read this question and the accepted answer: What is JavaScript garbage collection?
In the answer, Noldorin referenced some guidelines from Apple. Here is the part I'm concerned with:
Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection.
I'm always taking time to keep up-to-speed on best practices, especially if I can reduce the memory footprint of my scripts. So I went off to test some things. If I understand correctly, the following is an example of an object that deletes itself after invoking a method.
var thing = function () {
var a_method, and_another;
a_method = function() { /* do stuff */ };
and_another = function() { /* do some other stuff*/ };
this.init = function() { a_method(); and_another(); };
};
delete new thing().init();
Usually I'll wrap everything in a self invoking function and pass in my globals just like above. Everything is the same as I would normally do it, the only difference being that I added the delete
right before the new
.
The code works either way.
So the question is: Am I doing anything here? Is there some kind of benefit to deleting a reference to an object that only exists inside a function scope? Or am I just making things look confusing?
To the best of my knowledge, JavaScript's objects are garbage collected periodically when there are no references remaining to the object. It is something that happens automatically, but if you want to see more about how it works, at the C++ level, it makes sense to take a look at the WebKit or V8 source code
Detailed examples to follow. There’s a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that have become unreachable. Here the arrow depicts an object reference. The global variable "user" references the object {name: "John"} (we’ll call it John for brevity).
The basic garbage collection algorithm is called “mark-and-sweep”. The following “garbage collection” steps are regularly performed: The garbage collector takes roots and “marks” (remembers) them. Then it visits and “marks” all references from them. Then it visits marked objects and marks their references.
Garbage collection is a process that is implemented automatically. It can’t be forced or prevented anyhow. Objects can be retained in memory while they are reachable. It’s essential to know that being referenced is not similar to being reachable. Advanced algorithms of garbage collection are performed by modern engines.
First of all the statement delete new scoped_object().init();
is not really doing anything, you should better take care about what variables remain in-closure or if you have circular references, which are the most common source of memory leaks.
The delete
operator is meant to be used to delete object properties, and it is really misunderstood, the answer you quote from @Noldorin quotes some text of the Apple JavaScript "Best Practices", but they don't have a clue about how delete
works!!.
They even recommend using delete
on variable references, and that is not possible -only possible for variables declared in Eval Code-, because the var
statement declares the variable as non-deletable ({DontDelete}
in ECMAScript 3, or [[Configurable]] = false
in ECMAScript 5) properties of the Variable Object -objects that form the scope chain-.
Moreover, attempting to delete
a reference to an identifier that is bound to an environment record - an identifier declared with a VariableDeclaration
, FunctionDeclaration
or from a function's FormalParameterList
-, causes a SyntaxError
exception on the new ECMAScript 5th Edition under Strict Mode.
I would recommend you to read the following article about delete
:
delete
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With