JavaScript Stack Management / Freeing Up Memory

When defining a class as such:

var namespace = {}; namespace.appleClass = function() { this.collection = []; }; namespace.appleClass.prototype.methodOne = function(arg) { this.collection.push(arg); }; 

The way to get rid of this.collection is simple.

 this.collection.length = 0; 

My question is what happens to the methods described above (on namespace.appleClass.prototype ). How does JavaScript deal with them? Where is the function "stored"? How can this storage be freed?

The above class can be used as such.

 var apple = new appleClass(); appleClass.methodOne(2); appleClass.collection[0]// will be equal to two etc.. 

I know how null DOM links, COM links, and the long story behind the delete operator are. What I don't know is the best way to remove functions and references to them from a "user prototype" or "class" in order to say this.

+4
source share
2 answers

The way to get rid of this.collection is simple.

 this.collection.length = 0; 

This will not get rid of the array that just truncates it, so it has no entries. To completely get rid of the array, do delete this.collection (which will completely remove the property related to the array, making the array suitable for garbage collection) or this.collection = null (which gives a reference to the array, which makes it acceptable for garbage collection, but do not remove the property that will still exist with null ).

Everything that was in this.collection and which is not mentioned elsewhere will then be eligible for garbage collection. When (or indeed) this happens is completely dependent on the implementation of JavaScript.

And what’s the key point, in fact: “To manage memory in JavaScript, you just make sure that it means nothing that you don’t want anymore. It usually goes out of your application logic anyway (you don’t want it anymore, so you throw links to it.) If you are developing your code, bearing in mind how locks work , for the most part you do not need to worry about memory management.

For your questions in the comments to the question:

I take the methods found in namespace.appleClass.prototype . How does JavaScript deal with them? Where is the function "stored"? How can this storage be freed?

A functional object is stored on the heap (almost certainly, if only a local variable referred to it, some engines, such as Google V8, might use the stack for it); where the code is implementation dependent. The reference to the function object (and, therefore, its code) is stored in the prototype appleClass . There is only one of these functions, which is shared by all instances created by new namespace.appleClass through the prototype chain. There is no separate copy of a function for each instance (just a separate link to it), and therefore there is no need to worry about “clearing” this function of instances.

If you no longer need this function at all , somewhere in your code (for example, all appleClass instances are no longer needed), you can get rid of this via delete namespace.appleClass.prototype.methodOne ). This will remove the property from the prototype, which releases the reference to the function object, which releases the reference to the function code, all of which are then entitled to garbage collection. But that would be very unusual, and in any case, one function does not take up much memory.

+2
source

Setting the variable to null allows you to break any links to objects in all browsers, including circular links between DOM elements and Javascript areas. Using the delete command, we mark the objects to be cleaned the next time the Garbage collection is started, but if there are several variables that refer to the same object, deleting one variable DOES NOT release the object, it will simply remove the connection between this variable and the object. And the next time the garbage collection is started, only the variable will be cleared.

0
source

All Articles