From: Doug M. <do...@cr...> - 2001-02-21 21:21:08
|
eh? there IS a delete operator in JS. and saying "the specs say that mem is autoatically cleaned" is silly considering that we KNOW that it is not ----- Original Message ----- From: "Raymond Smith" <dst...@or...> To: <dyn...@li...> Sent: Wednesday, February 21, 2001 7:57 AM Subject: Re: [Dynapi-Dev] Continuing Freeing memory > More morning mind food (at least where I am)... > > > The new operator allocates memory for objects. How is this memory > deallocated? > > If you have ever programmed in C++, you might wonder why JavaScript does not > have a delete operator to match the new operator. The reason for this is > simple: It's not necessary! > In JavaScript, memory for objects is automatically reclaimed whenever there > is no longer need for the object. The JavaScript interpreter tracks > references to objects and deletes them when variables no longer refer to > them. > > For example, consider the following JavaScript code: > > d = new Array (50); > d = null; > > The first statement allocates memory for an Array object large enough to > hold 50 elements, and then records the fact that the new object is > referenced by a variable named d. > The next statement, however, changes d so that instead of referring to the > Array object, it instead contains the special value null. Since there is no > longer any way to access the Array object, the JavaScript interpreter > destroys it by reclaiming the memory that was allocated to it. > > Reclaiming memory allocated for objects which can no longer be accessed is > called garbage collection. Allocated objects which are no longer accessible > by variables are called orphan objects. > > This next example shows how to modify the above code to prevent the > destruction of the Array object: > > d = new Array (50); > e = d; // make e refer to the same object as d > d = null; // disassociate d from the Array object > Since the Array object is referenced by e at the moment it is abandoned by > d, the Array object remains intact. > In conclusion, there is no need to worry about destroying objects in > JavaScript. Garbage collection is performed automatically by the interpreter > whenever objects are no longer useful or documents are unloaded. If you want > to explicitly destroy an object, you can do so by abandoning it -- that is, > by making sure there are no variables that refer to it. (Setting the > variables to null works pretty well for this.) > > > > _______________________________________________ > Dynapi-Dev mailing list > Dyn...@li... > http://lists.sourceforge.net/lists/listinfo/dynapi-dev --- Outgoing mail is certified Virus Free by AVG Free Edition Download at: http://www.grisoft.com/html/us_index.cfm Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.231 / Virus Database: 112 - Release Date: 2/12/01 |