[Javascript] Garbage Collection/ Freeing-up memory in Java script

Troy III Ajnej trojani2000 at hotmail.com
Wed Sep 23 21:01:58 CDT 2009


Hello Dashant

 

There is an online resource that can answer your question and clear
up your suspicions on this matter.

(instead or interpreting) I will quote it here:

[quote start]
Instead of requiring manual deallocation, JavaScript relies on a technique called garbage collection. The JavaScript interpreter is able to detect when an object will never again be used by the program. When it determines that an object is unreachable (i.e., there is no longer any way to refer to it using the variables in the program), it knows that the object is no longer needed and its memory can be reclaimed. Consider the following lines of code, for example: 

 

var s = "hello";            // Allocate memory for a string
var u = s.toUpperCase( );  // Create a new string
s = u;                      // Overwrite reference to original string


After this code runs, the original string "hello" is no longer reachable -- there are no references to it in any variables in the program. The system detects this fact and frees up its storage space for reuse. 

Garbage collection is automatic and is invisible to the programmer. You can create all the garbage objects you want, and the system will clean up after you! You need to know only enough about garbage collection to trust that it works; you don't have to wonder about where all the old objects go. For those who aren't satisfied, however, Section 11.3, contains further details on the JavaScript garbage-collection process. 
[quote end]


Meaning that instead of writing something like:

 

 var hash = new Hashtable();
 hash.putValue("Key" + i, "Value" + i);
 delete hash;
 hash = null;

 

You could simply declare:

 

 Hashtable = new Hashtable() [the following code]

 

And the previous Hashtable will be automaticaly deleted by js garbage collector in its first turn.

 

Regards
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                      Troy III
                         progressive art enterprise
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 



 
> Date: Wed, 23 Sep 2009 12:16:59 +0530
> From: prashant.er at gmail.com
> To: javascript at lists.evolt.org
> Subject: [Javascript] Garbage Collection/ Freeing-up memory in Java script
> 
> Hi,
> 
> 
> 
> I’m trying with this type of code (example):
> 
> 
> 
> function Hashtable() {
> 
> 
> 
> this.valueList = new Array();
> 
> 
> 
> var somePrivateMethod = function() { return 1; }
> 
> 
> 
> this.putValue = function(name, value) {
> 
> 
> 
> this.valueList[name] = value;
> 
> 
> 
> }
> 
> 
> 
> }
> 
> 
> 
> 
> 
> 
> 
> for (var i = 0; i < 100000; i++)
> 
> 
> 
> {
> 
> 
> 
> var hash = new Hashtable();
> 
> 
> 
> hash.putValue("Key" + i, "Value" + i);
> 
> 
> 
> delete hash;
> 
> 
> 
> hash = null;
> 
> 
> 
> }
> 
> 
> 
> Running this script , it constantly increases the memory usage. Although the
> object "hash" is deleted in each iteration and although it is set to null,
> it does not seem to
> 
> be removed from memory. Calling $.summary() after execution has finished
> shows more than 100000 functions still in memory. Running the script a
> second time increases the memory again. More than 200000 functions are in
> memory after the second execution.
> 
> 
> 
> Am I doing anything wrong here ?
> 
> 
> 
> Any ideas, how I can get rid of the unreferenced memory? In fact, AFAIK JS
> engines do garbage collection automatically, any idea why does the java
> script engine does not release the memory immediately although it is
> unreferenced?
> 
> Is there a possibility to run the garbage collection of the java script
> engine ($.gc doesn’t helped much)?
> 
> 
> 
> Regards,
> 
> P
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
 		 	   		  
_________________________________________________________________
Bing™  brings you maps, menus, and reviews organized in one place.   Try it now.
http://www.bing.com/search?q=restaurants&form=MLOGEN&publ=WLHMTAG&crea=TEXT_MLOGEN_Core_tagline_local_1x1


More information about the Javascript mailing list